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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With