Note that there are some explanatory texts on larger screens.

plurals
  1. PORails partial in modal (render partial from different model)
    primarykey
    data
    text
    <p>I have tried to include a partial into a modal something similar to this <a href="http://rails-admin-tb.herokuapp.com/admin/divisions/new" rel="nofollow noreferrer">Rails Admin </a> when you click on "create language".</p> <p>Now as I have not really found out how the code works there, I tried to do a mix between this <a href="https://stackoverflow.com/questions/4065432/rails-ajax-a-modal-dialog">Rails - AJAX a Modal Dialog?</a> and something own.</p> <p>The thing is, the partial gets rendered now into the modal. But when I hit "save", the validation will go back to the "normal" create-new view and not show the validation errors directly within the modal.</p> <ol> <li><p>How can I show the validation errors in the popped up modal directly?</p></li> <li><p>When I save, I should somehow distinguish between a save from this modal and a normal save, as this partial will be called from somewhere else and would need to set the new created object directly as a new Object within a drop down. I guess i would need to distinguish from a response format and then use JS to set the new id to the DropDown id?</p></li> </ol> <p>Here some code written so far. In the view where I include the partial it looks like this where i call another controller (as I am on a different controllers-new-view).</p> <pre><code> $.get('&lt;%= url_for :controller =&gt; 'customers', :action =&gt; 'new' %&gt;', function(data) { $('#customer-modal').html(data); } ); </code></pre> <p>Then the new in the "customres" controller distinguishs between the different request format and renders the different partial (in this case "_ajax_form.html.erb"):</p> <pre><code>if request.xhr? render "_ajax_form", :layout =&gt; false </code></pre> <p>In this partial, I use "simple_form_for @customer, :remote => true do |f]....", so I guess at the end the submit should look different than standard, as this calls the normal "create" on the controller?</p> <pre><code>&lt;div class="modal-footer"&gt; &lt;%= f.button :submit, :class =&gt; 'btn primary' %&gt; &lt;a id='cancel-form' class= "btn small info"&gt;cancel&lt;/a&gt; &lt;/div&gt; </code></pre> <p>Thanks for any help which clarifies what I am doing wrong or how it should be done as my experience in Rails and JS/AXAX are not yet too experienced.. ;)</p> <ul> <li>EDITED: I have now a version where in my special partial which is loaded within the modal, will call a seperate action called "new_from_sale" within the simple_form_for:</li> </ul> <p><code>simple_form_for @customer, :url =&gt; url_for(:action =&gt; 'new_from_sale', :controller =&gt; 'customers') do |f|</code></p> <p>Then in the controller, when the save is ok of this new object, I redirect to the first called view with the new id as a param which then fills the dropdown. In the else case, when the new object can not be saved, I need somehow to show the errors still in that modal. So far both tries to either directly render the partial <code>render :partial =&gt; "customers/ajax_form"</code> or give back JS code <code>render :js =&gt; "$('#sale_customer_id').val(#{@customer.id});"</code> would not yet work.. but I'll keep on trying my luck..</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.
 

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