简体   繁体   English

在没有重定向的情况下,如何在Rails 2.3.5中执行另一个控制器的另一个动作(也渲染)?

[英]How can I perform another action of another controller (rendering too) in Rails 2.3.5 without a redirect?

I know that it's totally against the MVC, but anyway it must be done that way. 我知道这完全违反了MVC,但是无论如何都必须这样做。 So I need something like that: 所以我需要这样的东西:

app/controllers/controller_a.rb
class ControllerA < ApplicationController
  def index
    some_code
  end
end

app/controllers/controller_b.rb
class ControllerB < ApplicationController
  def other_index
    @var = 'example'
  end
end

app/views/controller_b/other_index.html.erb
<%= @var %>

So, when I visit the URL localhost:3000/controller_as/index (I mean, the one that corresponds the index action of controller_a.rb) I must obtain the next in my browser: 因此,当我访问URL localhost:3000 / controller_as / index(我的意思是,对应于controller_a.rb的索引操作的URL)时,我必须在浏览器中获取下一个:

example

I mean, I must perform controller_b other_index action and render other_index.html.erb 我的意思是,我必须执行controller_b other_index动作并呈现other_index.html.erb

I'll appreciate any help. 我将不胜感激。 Thanks. 谢谢。

You need to refactor the logic of controller_b into the model. 您需要将controller_b的逻辑重构到模型中。 Alternative would be to use AJAX to call controller_b from the index page of controller_a 替代方法是使用AJAX从controller_a的索引页调用controller_b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置Rails路由以使用参数和默认操作将某些URL重定向到另一个控制器? - How can I set rails routes to redirect some urls to another controller with parameter and default action? 来自另一个控制器的Rails3渲染动作 - Rails3 rendering action from another controller 我可以在另一个动作中调用动作(在rails控制器中)吗? - Can I call an action in another action (in a rails controller)? 在另一个控制器中渲染动作 - Rendering an action in another controller Rails-link_to-删除操作-如何使用link_to内部的另一个控制器操作重定向到另一个页面 - Rails - link_to - delete action - how to redirect to another page using another controller action inside a link_to Rails - 如何将创建/新操作从一个控制器放入另一个控制器的索引? - Rails - How can I put the create/new action from one controller into the index of another controller? 控制器如何响应从 Ruby 中的另一个网站重定向的操作 - How can a controller respond with an action to redirect from another website in Ruby Rails - 如何将变量从一个控制器动作移动到另一个控制器动作而不重置它 - Rails - how do I move a variable from one controller action to another without resetting it 如何重定向到相同控制器操作的另一种格式? - How to redirect to another format of the same controller action? 如何重定向以创建另一个控制器的操作 - How to redirect to create action of another controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM