开发者

how do i write action methods for partial views?

开发者 https://www.devze.com 2022-12-29 22:53 出处:网络
I rendering a view partially like th开发者_Python百科is. <%= render(:partial => \"index\" ,:controller=>\"controller_name\") %>

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号