Rails 4尝试将多个记录保存到单个模型

时间:2015-06-14 02:52:53

标签: ruby-on-rails-4 rails-activerecord simple-form omnicontacts-gem

我正在使用Omnicontacts Gem来提取联系人列表。到目前为止有效。我可以获取联系人并使用它们填充表单,以便我可以将它们传递给我的Contacts Controller以保存在联系人模型中。

我已经查看了Rails Cast#165的指导和Stackoverflow上的几篇帖子。但是我似乎无法破解这个坚果。感觉我错过了一些基本的东西,并且让我感到沮丧。

编辑:问题重述

我遇到的麻烦是没有联系人记录被写入内存。请参阅下面的更新日志部分。实际上,我正在尝试提交多条记录,只有最后一条记录被提取。这实际上并没有保存到模型中,然后导致其他问题。这似乎是由于调用NEW引起的。通过将方法更改为CREATE,它现在保存列表中的最后一项。但是,表单中未列出的任何字段都将保存为nil,这会导致其他问题。

从控制台运行:

$Contact.create([{ user_id: '1', first_name: 'sam'}, { user_id: '1', first_name: 'Dean'}]) 

在Contact模型中创建两个新记录。因此,我试图弄清楚如何从表单中实现这一点。基本上

可以在自己的模型中嵌套表单吗?我应该先将联系人保存到临时表吗?看来应该有一个“Rails”方式来做到这一点。任何意见是极大的赞赏。 该视图属于控制器的导入部分。

控制器:

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>

LOG:

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实际上并不存在,但我的应用程序确实存在。

2 个答案:

答案 0 :(得分:0)

尝试像这样更改视图代码

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

答案 1 :(得分:0)

所以我试图做的事情是不可能的。最终需要创建一个Import Controller,以便我可以通过User模型调用嵌套的Controller表单。

<div class="row">
  <div class="small-12">
    <%= simple_form_for(@user, url: import_path) do |f| %>
      <%= f.simple_fields_for :contacts, @contacts do |contact| %>
        <%= render 'contact_fields', f: contact %>
      <% end %>
    <div class="actions" align="right">
      <%= f.submit class:"button tiny" %>
    </div>
    <% end %>
  </div>
</div>