简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM