Mih*_*hai 26 erb ruby-on-rails-3
我需要渲染另一个控制器动作<%= render "controller/index" %>
,我得到这个错误
缺少部分控制器/索引{{formats => [:html],:locale => [:en,:en],:handlers => [:rjs,:rhtml,:rxml,:erb,:builder]} in查看路径"/ path_to/app/views"
如何将另一个控制器操作呈现到视图中但不向客户端发送重定向?我试过了
<%=render :action => "index", :controller=>"controller" %>
Run Code Online (Sandbox Code Playgroud)
但似乎没有用.
fl0*_*00r 30
尝试渲染模板:
<%= render :template => "controller/index" %>
Run Code Online (Sandbox Code Playgroud)
或文件:
<%= render :template => "#{Rails.root}/app/controllers/controller/index" %>
Run Code Online (Sandbox Code Playgroud)
我相信你应该通过控制器渲染它,只要它更方便:
def your_action
...
render :action => :index
end
Run Code Online (Sandbox Code Playgroud)
小智 21
这适合我:
def renderActionInOtherController(controller,action,params)
controller.class_eval{
def params=(params); @params = params end
def params; @params end
}
c = controller.new
c.request = @_request
c.response = @_response
c.params = params
c.send(action)
c.response.body
end
Run Code Online (Sandbox Code Playgroud)
然后,打电话给
render :text => renderActionInOtherController(OtherController,:otherAction,params)
Run Code Online (Sandbox Code Playgroud)
基本上它会攻击其他类并覆盖其"params"方法并返回
如果您使用的是Rails 4:
def renderActionInOtherController(controller,action,params)
c = controller.new
c.params = params
c.dispatch(action, request)
c.response.body
end
Run Code Online (Sandbox Code Playgroud)
Mar*_*ota 20
从Rails指南页面:
使用render with:action是Rails新手的常见混淆源.指定的操作用于确定要呈现的视图,但Rails不会在控制器中运行该操作的任何代码.在调用render之前,必须在当前操作中设置视图中所需的任何实例变量.
所以简而言之,你不能渲染另一个动作,你只能渲染另一个模板.您可以获取共享代码并将其移动到应用程序控制器中的方法.如果您真的无法以其他方式构建代码,那么您也可以尝试这一行:
# This is a hack, I'm not even sure that it will work and it will probably
# mess up your filters (like ignore them).
other_controller = OtherController.new
other_controller.request = @_request
other_controller.some_action
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40896 次 |
| 最近记录: |