有没有办法在同一页面上创建不嵌套在另一个页面中的多个模型?
例如,我想要一个可以创建用户的表单。它有两个简单的字段,名字和姓氏,显示在同一行。我希望能够添加一个链接“添加新用户”,该链接将创建(使用 javascript)一个相同的行而无需回发,并允许我在同一页面提交上创建两个用户。
我怎样才能使用rails实现这一点?
有没有办法在同一页面上创建不嵌套在另一个页面中的多个模型?
例如,我想要一个可以创建用户的表单。它有两个简单的字段,名字和姓氏,显示在同一行。我希望能够添加一个链接“添加新用户”,该链接将创建(使用 javascript)一个相同的行而无需回发,并允许我在同一页面提交上创建两个用户。
我怎样才能使用rails实现这一点?
添加字段并仅保留一个表单,一个提交按钮:
= form_tag(url: create_user_path, remote: true) do
%table
%tr
%td= text_field_tag 'user[][first_name]'
%td= text_field_tag 'user[][last_name]'
%tr.actions
%td= submit_tag 'Save'
%td= button_tag 'Add new user form', id: 'add_user_form'
%tr.new_user_row.hidden # hidden class matches the css rule: {display:none;}
%td= text_field_tag "user[][first_name]"
%td= text_field_tag "user[][last_name]"
:javascript # jQuery
$('#add_user_form').bind('click', function(e) {
var row = $('tr.new_user_row').clone().removeClass('hidden new_user_row');
$('tr.actions').before(row); # will append the <tr> before the actions
});
在用户控制器中:
def create
params[:user].each do |attr|
User.create(attr)
end
end
该行tr.new_user_row.hidden
用作新行的模板:通过单击按钮#add_user_form
,JS 代码将选择模板行,clone
并将具有空输入的新行添加为表格的最后一个可见行。
这里是users_controller.rb
def new
end
def create_multiple
params[:users].each do |user|
user = User.create(user)
end
redirect_to users_url
end
这里是new.html.erb
<%= form_tag '/users/create_multiple' do %>
<%= render 'user_fields' %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
这里是_user_fields.html.erb
<script type="text/javascript">
$(function() {
var scntDiv = $('#addusers');
var i = $('#addusers div').size() + 1;
$('#addfields').on('click', function() {
$('<div class="field"><h2>User ' + i +'</h2><input id="users__firstname' + i +'" name="users[][firstname]" placeholder="first name" type="text" /><input id="users__lastname' + i +'" name="users[][lastname]" placeholder="last name" type="text" /></div>').appendTo(scntDiv);
i++;
return false;
});
});
</script>
<div id="addusers">
<div class="field">
<h2>User 1</h2>
<%= text_field_tag "users[][firstname]", nil, :placeholder => "first name" %>
<%= text_field_tag "users[][lastname]", nil, :placeholder => "last name" %>
</div>
</div>
<a href="#" id="addfields">Add a New User</a><br/>
日志结果
Started POST "/users/create_multiple" for 127.0.0.1 at 2013-06-05 00:40:07 +0700
Processing by UsersController#create_multiple as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"xOPM6PB1h6DMUEGS7fX9/eWs/e6dg
XKRj231ReviKFo=", "users"=>[{"firstname"=>"test1", "lastname"=>"last1"}, {"first
name"=>"test2", "lastname"=>"last2"}], "commit"=>"Save changes"}
←[1m←[36m (78.0ms)←[0m ←[1mbegin transaction←[0m
←[1m←[35mSQL (49.0ms)←[0m INSERT INTO "users" ("created_at", "firstname", "la
stname", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 04 Jun 2013 17:
40:08 UTC +00:00], ["firstname", "test1"], ["lastname", "last1"], ["updated_at",
Tue, 04 Jun 2013 17:40:08 UTC +00:00]]
←[1m←[36m (7.0ms)←[0m ←[1mcommit transaction←[0m
←[1m←[35m (0.0ms)←[0m begin transaction
←[1m←[36mSQL (3.0ms)←[0m ←[1mINSERT INTO "users" ("created_at", "firstname",
"lastname", "updated_at") VALUES (?, ?, ?, ?)←[0m [["created_at", Tue, 04 Jun 2
013 17:40:08 UTC +00:00], ["firstname", "test2"], ["lastname", "last2"], ["updat
ed_at", Tue, 04 Jun 2013 17:40:08 UTC +00:00]]
←[1m←[35m (5.0ms)←[0m commit transaction
Redirected to http://localhost:3000/users
Completed 302 Found in 156ms (ActiveRecord: 142.0ms)
您可以根据自己的意愿添加验证代码,这是为多个记录基本结构传递表单参数的方法