In the same manner, the create method needs to call
form before rendering the new view:
def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = 'Article was successfully created.'
redirect_to :action => 'list'
else
form
render :action => 'new'
end
end
edit works the same way too:
def edit
@article = Article.find(params[:id])
form
end
Finally, edit the update method, which will change some when you add the ability to
preview an article. However, right now it should look like this:
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = 'Article was successfully updated.'
redirect_to :action => 'list'
else
form
render :action => 'edit'
end
end
Once again, adding the invocation of the form method is the only thing that has changed.
Ideally, your controller should now be in good shape for working with articles, but you still
CHAPTER 7 ?– A RAILS CMS 137
need to edit the views. The list view is close to the original, except that you only display
select elements. For example, it isn??™t a good idea to display the article content in the list:
Listing articles