rails reusing view templates
A rails newbie here
I 开发者_开发技巧have 2 actions in my controller 1) index 2) refine_existing. Both of them show the results in the same format. How do I reuse the index.html.erb file?
When I try the following, it complains about refine_existing.erb not being present.
def refine_existing
...
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @results }
end
end
my index action looks like this
def index
#some logic to get @results
#set some session variables etc.
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @results }
end
end
Do I have to refactor my index view to contain partials that
a) make the headers b) render @results and reuse them? Even though, both index.html.erb and refine_existing.html.erb will look exactly the sameIs there any way I can say in my refine_existing action to use index.erb view?
thanks in advance
By convention, if you don't specify a template name Rails looks for one matching the action. You can override this by calling render
explicitly with the desired template name. The only wrinkle is that the path is relative to TEMPLATE_ROOT, which is normally app/views
:
def refine_existing
...
respond_to do |format|
format.html { render :template => "<table_name>/index.html.erb" }
end
end
replacing table_name
with the "tablized" form of the model. E.g. if your controller is PostsController
, then posts
. So your template would then live in app/views/posts/index.html.erb
-- if you've customized paths somehow adjust as necessary.
精彩评论