Rails的部分模态(呈现来自不同型号的部分) [英] Rails partial in modal (render partial from different model)

查看:102
本文介绍了Rails的部分模态(呈现来自不同型号的部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试包括部分为与此类似 Rails的管理模态的东西 当你点击创建语言。

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

现在,我还没有真正找到了如何在code那里工作,我试图做到这一点的混合体的 Rails的? - AJAX一个模式对话框和自己的东西。

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. 我怎样才能显示验证错误弹出的模式直接?

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

当我保存,我应该以某种方式区分一个挽救这个模式和正常保存,因为这部分会从别的地方调用,将需要直接设置新创建的对象中的一个新对象下拉。我想我会需要从响应格式区分,然后用JS新的ID设置为下拉ID?

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?

下面一些code到目前为止已写入。在视图中,我包括它看起来像这样的地方我调用另一个控制器(因为我在不同的控制器,新的视图)的部分。

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

那么新的不同的请求格式之间的customres控制器distinguishs并呈现不同的部分(在这种情况下,_ajax_form.html.erb):

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

在此部分中,我使用simple_form_for @customer,:远程=>真正办| F] ......,所以我想在年底提交应该比标准的不同,因为这调用普通创建在控制器上?

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>

感谢您的这阐明什么,我做错了,或者应该怎么做我的Rails中的经验和JS / AXAX还没有太多经验的帮助..;)

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

  • 编辑:我现在是在模式中加载一个版本,其中我在特别的部分,将调用simple_form_for内称为new_from_sale一个单独的行动:

simple_form_for @customer,:URL =&GT; url_for(:动作=&GT;new_from_sale',:控制器=&GT;客户)做| F |

然后在控制器中,当保存是确定这个新对象,我重定向到第一次调用视图中使用新的id作为一个参数,然后填充下拉列表。在其他情况下,当新的对象不能保存,我需要以某种方式仍显示在该模式中的错误。 到目前为止,双方试图直接呈现偏渲染:部分=&GT; 客户/ ajax_form或归还JS code 渲染:JS =&GT; $('#sale_customer_id)VAL(#{@ customer.id});将没有工作..但我会继续尝试我的运气。

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

推荐答案

这里有一个如何我会做我写响应类似的问题模式窗体(的 Rails和jQuery的模态对话框的形式):

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

您需要添加:远程=&GT;真正的到被加载到对话框的形式。这将导致对JS格式必须从创建动作呈现。然后,您可以处理在create.js.erb的响应。

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.

这篇关于Rails的部分模态(呈现来自不同型号的部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆