how do i write action methods for partial views?
I rendering a view partially like th开发者_Python百科is.
<%= render(:partial => "index" ,:controller=>"controller_name") %>
so this will partially render controller_name/_index.html.erb
here is my doubt. can i write an action method for this _index. something like this?
class ControllerNameController < ApplicationController
def _index
end
end
thanks.
No this should be
class ControllerNameController < ApplicationController
def index
render :partial=>'index'
end
end
EDITED: Explaining my answer in detail -
When you write a method method_name
and you do not render
( redirect_to
) anything, the controller will look for page method_name.html.erb
by default.
However, using render :partial
as shown below, the action will work with the partial instead.
For Example
class ControllerNameController < ApplicationController
def some_method_name
render :partial=>'index' #look for the _index.html.erb
end
end
class ControllerNameController < ApplicationController
def some_method_name
render :action=>'index' #look for the index.html.erb
end
end
class ControllerNameController < ApplicationController
def some_method_name #look for the "some_method_name.html.erb"
end
end
精彩评论