我正在使用 Omnicontacts Gem 来提取联系人列表。到目前为止,这行得通。我可以获取联系人并使用它们来填充表单,这样我就可以将它们传递给我的联系人控制器以保存在联系人模型中。
我查看了 Rails Cast #165 的指导以及 Stackoverflow 上的几篇文章。但是,我似乎无法破解这个坚果。感觉就像我错过了一些基本的东西,让我发疯。
编辑:问题重述
我遇到的麻烦是零联系记录被写入内存。请参阅下面的更新日志部分。实际上,我正在尝试提交多条记录,只有最后一条被提取。这实际上并没有保存到模型中,这会导致其他问题。看来这是由调用 NEW 引起的。通过将方法更改为 CREATE,它现在保存了列表中的最后一项。但是,表单中未列出的任何字段都保存为 nil,这会导致其他问题。
从控制台工作:
$Contact.create([{ user_id: '1', first_name: 'sam'}, { user_id: '1', first_name: 'Dean'}])
在联系人模型中创建两条新记录。所以我试图弄清楚如何通过表格实现这一点。基本上。
可以在自己的模型中嵌套表格吗?我应该先将联系人保存到临时表吗?看起来应该有一种“Rails”的方式来做到这一点。任何意见是极大的赞赏。该视图属于控制器的 Import 部分。
控制器:
def new
@contact = Contact.new
redirect_to action: 'index'
end
def edit
end
def create
@user = current_user
@contact = @user.contacts.new(contact_params)
@contact.save
redirect_to action: 'index'
end
def import
@import = request.env['omnicontacts.contacts']
respond_to do |format|
format.html
end
end
看法:
<div class="row">
<div class="small-12">
<%= simple_form_for Contact.new do |f| %>
<% unless @import.nil? %>
<% @import.each do |c| %>
<%= f.input :first_name, input_html: {value: c[:first_name]} %>
<%= f.input :last_name, input_html: {value: c[:last_name]} %>
<%= f.input :email_home, input_html: {value: c[:email]} %>
<%= f.input :phone_home, input_html: {value: c[:phone_number]} %>
<% end %>
<% end %>
<div class="actions" align="right">
<%= f.button :submit %>
</div>
<% end %>
</div>
</div>
日志:
Started POST "/contacts" for ::1 at 2015-06-14 06:38:52 -0500
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UFadEXa3yeTmQ/ySsXcRhLmt9Bulr4hp0dlPc5N6LIlYKlRPkdgwZ0tgOSgBv9eD38Ng5U2XC4zJFlIuwcpHfA==", "contact"=>{"first_name"=>"omniplan-crash", "last_name"=>"", "email_home"=>"omniplan-crash@omnigroup.com", "phone_home"=>""}, "commit"=>"Create Contact"}
(0.1ms) BEGIN
SQL (0.5ms) INSERT INTO "contacts" ("first_name", "last_name", "email_home", "phone_home", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["first_name", "omniplan-crash"], ["last_name", ""], ["email_home", "omniplan-crash@omnigroup.com"], ["phone_home", ""], ["user_id", 1], ["created_at", "2015-06-14 11:38:52.239022"], ["updated_at", "2015-06-14 11:38:52.239022"]]
SQL (0.3ms) UPDATE "users" SET "contacts_count" = COALESCE("contacts_count", 0) + 1 WHERE "users"."id" = $1 [["id", 1]]
(1.1ms) COMMIT
Redirected to http://localhost:3000/contacts
Completed 302 Found in 12ms (ActiveRecord: 2.1ms)
Started GET "/contacts" for ::1 at 2015-06-14 06:38:52 -0500
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Processing by ContactsController#index as HTML
(0.6ms) SELECT "contacts"."id" FROM "contacts" WHERE "contacts"."user_id" = $1 [["user_id", 1]]
Contact Load (2.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" IN (4, 6, 7, 8, 9, 13, 14, 29, 31, 32, 34, 35, 37, 39, 51, 52, 53, 55, 56, 69, 71, 79, 91, 97, 105, 106, 107, 112, 113, 114, 119, 120, 126, 136, 141, 152, 157, 158, 162, 176, 189, 194, 198, 205, 207, 220, 221, 226, 232, 235, 238, 240, 244, 246, 249, 254, 257, 261, 262, 266, 272, 289, 290, 291, 309, 327, 338, 342, 350, 361, 363, 364, 375, 376, 386, 387, 389, 393, 394, 402, 413, 416, 418, 422, 432, 434, 440, 442, 444, 446, 447, 452, 456, 459, 465, 487, 492, 494, 497, *501*)
Contact Load (3.8ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = $1 ORDER BY "contacts"."last_name" ASC, "contacts"."first_name" ASC [["user_id", 1]]
当我检查我的表记录 501 时实际上并不存在,但我的应用程序确实存在。