0

I have a page where is displayed information about a respective car (it's a controller cars, action show). On this page, I render _form from the controller called reviews.

When I send out the form (through AJAX), the review is successfully created, but I am not able from the reviews controller render any action from the cars controller - to be more specific, here's the code:

# reviews_controller
  def create
    @review = Review.new(review_params)
    @review.save
    #respond_with(@review)
    respond_to do |format|
      format.js { render :action => "/cars/car_profile" }
    end
  end

The error I get:

ActionView::MissingTemplate (Missing template reviews/cars/car_profile, application/cars/car_profile with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/Users/adam/rubydev/claisy/app/views"
  * "/Users/adam/.rvm/gems/ruby-2.0.0-p481/gems/devise-3.4.1/app/views"
):
  app/controllers/reviews_controller.rb:28:in `block (2 levels) in create'
  app/controllers/reviews_controller.rb:27:in `create'

How can I render in controller an action that is located in a different controller?

Thank you in advance.

1
  • Did you try to render :template instead of :action?
    – lifus
    Jan 31, 2015 at 9:42

1 Answer 1

0

:action forces rails to search for templates within the current controller view folder.

Missing template reviews/cars/car_profile

You should write this instead

render "cars/car_profile"

See point 2.2.3

What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with render, which accepts the full path (relative to app/views) of the template to render.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.