Search code examples
ruby-on-railsruby-on-rails-3.1actioncontroller

Rendering Rails partial view (with issues reading the Rails API)


I have a jQuery post submitting a form to a controller create action, which works great. If the save was successful I would like the create action to return a different form so the success callback will insert this form. Is this possible?

Here's my code:

def create
    @event = Event.new(params[:event])

      if @event.save
        # This is where I would like to render a different controller action's view.
        render :controller => "shows", :action => "new", :layout => false
      else
        render action: "new"
      end
  end

For some reason it will not render the "shows/new" template. It keeps rendering the current controller's new template without the layout. What am I missing here?

As an aside, I had a look at api.rubyonrails.org and tried to look up the render method. I found it listed as render(context,options), but can't for the life of me find out what the valid options are. This seems to be a common pattern for a lot of methods. How do I find out? It will certainly help me figure out what my options are, and perhaps give various things a try.

Thanks, Dany.

ADDED: I have now used render "shows/new", :layout => false in my controller action, which is working. In my new.html.erb for Shows I have declared <%= render "/shows/form" %>. Unfortunately I am now getting 500 error. I found this in development.log:

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= form_for(@show) do |f| %>
    2:   <% if @show.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(@show.errors.count, "error") %> prohibited this show from being saved:</h2>
  app/views/shows/_form.html.erb:1:in `_app_views_shows__form_html_erb___1397093944823648986_2158339140'
  app/views/shows/new.html.erb:3:in `_app_views_shows_new_html_erb__1152608637968596369_2158584080'
  app/controllers/events_controller.rb:61:in `create'

I'm not entirely sure what's causing this...


Solution

  • Seems you missed: 2.2.3 Rendering an Action’s Template from Another Controller

    Try:

     render "shows/new", :layout => false