Note that there are some explanatory texts on larger screens.

plurals
  1. PORails modal dialog that creates record then updates page on close
    primarykey
    data
    text
    <p>I am learning Rails by making a simple recipe tracking program. Here's where I'm stuck: when I am on the form to create a new recipe in the system, if that recipe's source (ie a book, magazine, etc) doesn't currently exist I would like to create that too at the same time. In other words when I am looking at 'recipe/new.html', if I need to add a new source, using jQuery dialog (or another approach if that makes more sense) I would like to render 'source/_form.html.erb' (the partial for a new recipe source), create a new recipe source, and then inject that new record at the end of my select box.</p> <p>From recipe/new.html:</p> <pre><code>&lt;div class="field"&gt; &lt;%= f.label :source %&gt;&lt;br /&gt; &lt;%= select("recipe", "source_id", Source.all.sort_by(&amp;:title).collect {|s| [ s.title, s.id ] }, { :include_blank =&gt; true }) %&gt; &lt;%= link_to "new source", new_source_path (@source, :format =&gt; :js), :remote =&gt; true %&gt; &lt;/div&gt; </code></pre> <p>From sources/new.js.erb</p> <pre><code>$("&lt;%= escape_javascript render(:partial =&gt; 'sources/form') %&gt;").dialog(); $('#new_comment_link').hide(); </code></pre> <p>From controllers/sources_controller.rb</p> <pre><code> # GET /sources/new # GET /sources/new.xml def new @source = Source.new respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @source } format.js end end # GET /sources/1/edit def edit @source = Source.find(params[:id]) end # POST /sources # POST /sources.xml def create @source = Source.new(params[:source]) respond_to do |format| if @source.save format.html { redirect_to(@source, :notice =&gt; 'Source was successfully created.') } format.xml { render :xml =&gt; @source, :status =&gt; :created, :location =&gt; @source } else format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @source.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>The sources/form partial, which should render in jquery dialog then disappear on submit. When dialogue closes I want the new record created by this form to appended to the end of my select box on recipe/new.html:</p> <pre><code>&lt;%= form_for(@source) do |f| %&gt; &lt;% if @source.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@source.errors.count, "error") %&gt; prohibited this source from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @source.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :author_last %&gt;&lt;br /&gt; &lt;%= f.text_field :author_last %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :author_first %&gt;&lt;br /&gt; &lt;%= f.text_field :author_first %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :media_type %&gt;&lt;br /&gt; &lt;%= f.text_field :media_type %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Thank you for taking the time to read through this.</p> <p><strong>EDIT:</strong> To be more explicit about what my problem is, the code above renders a dialog and allows me to submit a new source. However, after submission it redirects to the new source's view page, rather than the new recipe form I started on before I opened the dialog.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload