Rails 3 从另一个控制器渲染动作

发布于 2024-11-08 15:03:00 字数 415 浏览 5 评论 0原文

我需要渲染另一个控制器操作 <%= render "controller/index" %> 我收到这个错误

缺少部分控制器/索引 {:formats=>[:html], :locale=>[:en, :en], :handlers=>[:rjs, :rhtml, :rxml, :erb , :builder]} 在视图路径“/path_to/app/views”

如何将另一个控制器操作渲染到视图中而不向客户端发送重定向? 我已经尝试过

<%=render :action => "index", :controller=>"controller" %>

,但似乎不起作用。

I need to render another controller action <%= render "controller/index" %>
and i get this error

Missing partial controller/index with {:formats=>[:html], :locale=>[:en, :en], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder]} in view paths "/path_to/app/views"

how can i render another controller action into a view but without sending an redirect to the client ?
I've tried

<%=render :action => "index", :controller=>"controller" %>

but it seems that is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如果没有你 2024-11-15 15:03:00

尝试渲染 template:

<%= render :template => "controller/index" %> 

或 file:

<%= render :template => "#{Rails.root}/app/controllers/controller/index" %> 

我相信你应该通过控制器渲染它,因为它更方便:

def your_action
  ...
  render :action => :index
end

Try to render template:

<%= render :template => "controller/index" %> 

Or file:

<%= render :template => "#{Rails.root}/app/controllers/controller/index" %> 

And I believe you should render it through controller, as far as it is more convenient:

def your_action
  ...
  render :action => :index
end
俏︾媚 2024-11-15 15:03:00

这对我来说效果很好:

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

然后,基本上调用

render :text => renderActionInOtherController(OtherController,:otherAction,params)

它会破解另一个类并覆盖其“params”方法并返回

如果您使用的是Rails 4:

def renderActionInOtherController(controller,action,params)
    c = controller.new
    c.params = params
    c.dispatch(action, request)
    c.response.body
end

This works well for me :

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

then, call by

render :text => renderActionInOtherController(OtherController,:otherAction,params)

basically it hacks the other class and overwrites its "params" method and return

If you are using Rails 4:

def renderActionInOtherController(controller,action,params)
    c = controller.new
    c.params = params
    c.dispatch(action, request)
    c.response.body
end
献世佛 2024-11-15 15:03:00

来自 Rails 指南页面

将渲染与 :action 一起使用是
Rails 的常见混乱源
新人。指定的动作是
用于确定要查看哪个视图
渲染,但 Rails 不运行任何
该操作的代码
控制器。任何实例变量
您在视图中需要的必须是
在当前动作之前设置
调用渲染。

简而言之,您无法渲染另一个动作,只能渲染另一个模板。您可以获取共享代码并将其移动到应用程序控制器中的方法。如果您确实无法以其他方式构建代码,您也可以尝试以下方法:

# 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

From Rails Guides page:

Using render with :action is a
frequent source of confusion for Rails
newcomers. The specified action is
used to determine which view to
render, but Rails does not run any of
the code for that action in the
controller. Any instance variables
that you require in the view must be
set up in the current action before
calling render.

So in short you can't render another action, you can only render another template. You could get the shared code and move it to a method in application controller. You could also try something along this lines if you really can't structure your code in some other way:

# 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
⊕婉儿 2024-11-15 15:03:00

如果您不想只渲染另一个控制器(/model)的view,而是调用action(方法),请更多地考虑Ruby的生活方式 - put将此方法放入模块中,并将其包含在您需要的控制器中。

我认为它不像触摸其他控制器那样“诡异”。

module StandardActions
    def show_user_homepage(local_params=params)
        #something finding 
        #something to check
        render :"homepage/show" 
    def
end

class AddressesController < ApplicationController
    include StandardActions

    def update
        # update address
        if ok
            show_user_homepage(id: user_id)
        else
            #errorthings
            render :edit #(eg.)
        end
    end         
end

class HobbiesController  < ApplicationController
    include StandardActions

    def update      
        # update hobby
        if ok
            show_user_homepage(id: user_id)
        else
            #errorthings
            render :edit #(eg.)
        end
    end         
end

If you do not want just to render the view of the other controller (/model), but calling the action (method), think more the ruby way of life - put this method(s) into a module and include it in the controllers you need it.

I think its less 'spooky' then somehow touching an other controller.

module StandardActions
    def show_user_homepage(local_params=params)
        #something finding 
        #something to check
        render :"homepage/show" 
    def
end

class AddressesController < ApplicationController
    include StandardActions

    def update
        # update address
        if ok
            show_user_homepage(id: user_id)
        else
            #errorthings
            render :edit #(eg.)
        end
    end         
end

class HobbiesController  < ApplicationController
    include StandardActions

    def update      
        # update hobby
        if ok
            show_user_homepage(id: user_id)
        else
            #errorthings
            render :edit #(eg.)
        end
    end         
end
深居我梦 2024-11-15 15:03:00

渲染另一个控制器的部分内容是没有问题的。
那么为什么不将原始操作视图移动到局部视图并在需要的地方渲染此局部视图:

将 app/views/controller_a/index.html.erb 移动到
app/views/controller_a/_index.html.erb 。

应用程序/视图/controller_a/index.html.erb:

<%= render partial: 'index' %>

应用程序/视图/controller_b/some_action.html.erb:

<%= render partial: 'controller_a/index' %>

Rendering partials of another controller is no problem.
So why not moving the original action view to a partial and render this partial wherever you need it:

move app/views/controller_a/index.html.erb to
app/views/controller_a/_index.html.erb .

app/views/controller_a/index.html.erb:

<%= render partial: 'index' %>

app/view/controller_b/some_action.html.erb:

<%= render partial: 'controller_a/index' %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文