简体   繁体   English

Rails偏模态(从不同模型渲染部分)

[英]Rails partial in modal (render partial from different model)

I have tried to include a partial into a modal something similar to this Rails Admin when you click on "create language". 当你点击“创建语言”时,我试图将一个部分包含在类似于这个Rails Admin的模态中。

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? 现在因为我还没有真正发现代码是如何工作的,我试着在这个Rails - AJAX模态对话框之间进行混合 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? 我想我需要区分响应格式然后使用JS将新ID设置为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"): 然后,“customres”控制器中的new区分不同的请求格式并呈现不同的部分(在本例中为“_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? 在这部分中,我使用“simple_form_for @customer,:remote => true do | f] ....”,所以我猜最后提交应该看起来与标准不同,因为这称为正常的“创建”控制器?

<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.. ;) 感谢任何帮助澄清我做错了什么或者应该怎么做,因为我在Rails和JS / AXAX方面的经验还不太有经验..;)

  • 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中调用一个名为“new_from_sale”的单独操作:

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. 然后在控制器中,当这个新对象的保存正常时,我重定向到第一个被调用的视图,其中新的id作为param然后填充下拉列表。 In the else case, when the new object can not be saved, I need somehow to show the errors still in that modal. 在else的情况下,当无法保存新对象时,我需要以某种方式显示仍然在该模态中的错误。 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});" 到目前为止,两者都试图直接渲染部分render :partial => "customers/ajax_form"或者返回JS代码render :js => "$('#sale_customer_id').val(#{@customer.id});" would not yet work.. but I'll keep on trying my luck.. 还不行..但我会继续试试运气..

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 ): 这是我如何做一个模态表单的基本示例,我在回答类似问题( Rails和模态jquery对话框表单 )时写了这个表单

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

You need to add :remote => true to the form that gets loaded into the dialog. 您需要将:remote => true添加到加载到对话框中的表单中。 That will cause the JS format to be rendered from the create action. 这将导致JS格式从create动作呈现。 You can then handle the response in a create.js.erb. 然后,您可以在create.js.erb中处理响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM