Rails partial in modal (render partial from different model)

给你一囗甜甜゛ 提交于 2019-12-12 08:09:15

问题


I have tried to include a partial into a modal something similar to this Rails Admin when you click on "create language".

Now as I have not really found out how the code works there, I tried to do a mix between this Rails - AJAX a Modal Dialog? and something own.

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.

  1. How can I show the validation errors in the popped up modal directly?

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

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

 $.get('<%= url_for :controller => 'customers', :action => 'new' %>',
            function(data) {
                $('#customer-modal').html(data);
            }
    );

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"):

if request.xhr?
  render "_ajax_form", :layout => false

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?

<div class="modal-footer">
    <%= f.button :submit, :class => 'btn primary' %>
    <a id='cancel-form' class= "btn small info">cancel</a>
  </div>

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

  • 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:

simple_form_for @customer, :url => url_for(:action => 'new_from_sale', :controller => 'customers') do |f|

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 render :partial => "customers/ajax_form" or give back JS code render :js => "$('#sale_customer_id').val(#{@customer.id});" would not yet work.. but I'll keep on trying my luck..


回答1:


Here's a basic example of how I'd do a modal form which I wrote in response to a similar question (Rails and modal jquery dialog form):

https://github.com/ramblex/modal-form

You need to add :remote => true to the form that gets loaded into the dialog. That will cause the JS format to be rendered from the create action. You can then handle the response in a create.js.erb.



来源:https://stackoverflow.com/questions/8204705/rails-partial-in-modal-render-partial-from-different-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!