在rails的同一模型的新视图中追加部分表单 [英] Append form partial in the new view of the same model in rails

查看:167
本文介绍了在rails的同一模型的新视图中追加部分表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用同一个模型在一个视图(new.erb.html)中创建更多记录,同时提交一次。

例如,

我有一个名为Report的模型,其中包含一些字段,例如标题,描述和日期。

当我单击添加按钮时,如何可以多次渲染相同的表单?我认为这不是一个被模仿的问题,因为它始终是相同的模型动作。
Thanks $ / $>

解决方案

你可以这样做(我没有自己测试代码):





  .model 
%input {:name = > model [] [name]}
%input {:name => model [] [desc]}
%input {:name => model [] [date]}
.container
%button.add-model

js:

  $('。add-model')。click(function(){
$('。model')。clone()。first()。appendTo('。container');
})

控制器:

  def保存
params [:model] .each do |道具|
Model.create(道具)
结束
结束


i'd like to use the same model to create in one view (new.erb.html) more records at once with a single submit.

for example I've a model called Report with some fields such title, description and date

how can render same form partial many times when i click an add button? i think is not a nasted model's problem because is always the same model actions. Thanks

解决方案

You can do something like this (I haven't test the code myself):

haml:

.model
  %input{:name => "model[][name]"}
  %input{:name => "model[][desc]"}
  %input{:name => "model[][date]"}
.container
%button.add-model

js:

$('.add-model').click(function(){
  $('.model').clone().first().appendTo('.container');
})

controller:

def save
   params[:model].each do |props|
     Model.create(props)
   end
end

这篇关于在rails的同一模型的新视图中追加部分表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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