Now that that release candidate for rails 1.2 is out, people are going to have even more reasons to use the respond_to method in their controller methods. With that, there may come some unsettling feeling that you’re just repeating yourself over and over again.
def index
@articles = Article.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => @articles.to_xml }
end
end
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @article.to_xml }
end
end
If we want to use the respond_to, we have to make sure it’s in all of our methods. Wouldn’t it be easier to just do something like this?
def index
@articles = Article.find(:all)
format.html
format.xml { render :xml => @articles.to_xml }
end
Doing it this way, we don’t have to write 2 extra lines per method, and we don’t increase our indentation. While that’s not a huge thing, I still just don’t like to constantly write out those lines when I don’t have to when I think we can build something a little trickier.