1

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

codedog
  • 2,488
  • 9
  • 38
  • 67

1 Answers1

2

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

Try:

 render "shows/new", :layout => false
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Wonder how I missed that. Thanks - half way there! Now I have a weirdness where it renders the correct template, but not the correct "_form" within that template. It's not rendering "shows/_form" within "shows/new" – codedog Nov 01 '11 at 21:58
  • ok it's because the controller is still the bad one, you should provide absolute path in the render of your partial – apneadiving Nov 01 '11 at 21:59
  • Are you referring to `2.2.4 Rendering an Arbitrary File`? – codedog Nov 01 '11 at 22:04
  • no I'm talking about partials in views so `3.4.1 Naming Partials` – apneadiving Nov 01 '11 at 22:06
  • Ok, in `shows > new.html.erb` I now have `<%= render 'shows/form' %>`. Now I get 500 error. – codedog Nov 01 '11 at 22:17
  • That's getting better, it's targeting the right _form now. I'm still stuck though - I have edited the question to include the error. Thanks for all your help so far, I'm getting really close. – codedog Nov 01 '11 at 22:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4650/discussion-between-apneadiving-and-danyw) – apneadiving Nov 01 '11 at 22:36