如何在不重定向的情况下在 Rails 2.3.5 中执行另一个控制器的另一个操作(也进行渲染)?
我知道这完全违背了 MVC,但无论如何它必须这样做。所以我需要这样的东西:
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 %>
所以,当我访问 URL localhost:3000/controller_as/index (我的意思是,对应于controller_a.rb 的索引操作的 URL)时,我必须在浏览器中获取下一个:
example
我的意思是,我必须执行controller_b other_index操作并渲染other_index.html.erb
我将不胜感激。谢谢。
I know that it's totally against the MVC, but anyway it must be done that way. 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:
example
I mean, I must perform controller_b other_index action and render other_index.html.erb
I'll appreciate any help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将controller_b的逻辑重构到模型中。另一种方法是使用 AJAX 从controller_a的索引页调用controller_b
You need to refactor the logic of controller_b into the model. Alternative would be to use AJAX to call controller_b from the index page of controller_a