我如何为部分视图编写动作方法?

ZX1*_*12R 1 ruby-on-rails

我渲染的视图部分是这样的.

<%= render(:partial => "index" ,:controller=>"controller_name") %>
Run Code Online (Sandbox Code Playgroud)

所以这将部分呈现controller_name/_index.html.erb

这是我的疑问.我能为这个_index写一个动作方法吗?这样的事情?

class ControllerNameController < ApplicationController
  def _index
  end
end
Run Code Online (Sandbox Code Playgroud)

谢谢.

Sal*_*lil 10

不,这应该是

class ControllerNameController < ApplicationController
  def index
   render :partial=>'index'
  end
end
Run Code Online (Sandbox Code Playgroud)

编辑:详细解释我的答案 - 当你编写一个方法method_name而你没有render(redirect_to)任何东西时,控制器method_name.html.erb默认会查找页面.

但是,使用render :partial如下所示,该操作将与partial一起使用.

例如

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
Run Code Online (Sandbox Code Playgroud)