0

Omnicontacts Gem を使用して、連絡先のリストを取得しています。これまでのところ、それは機能します。連絡先を取得し、それを使用してフォームにデータを入力できるので、それらを連絡先コントローラーに渡して連絡先モデルに保存できます。

Rails Cast #165 のガイダンスと Stackoverflow に関するいくつかの投稿を確認しました。しかし、私はこのナットを割ることができないようです. 基本的な何かが欠けているように感じ、バタバタしています。


編集: 再記述を発行

私が抱えている問題は、ゼロの連絡先レコードがメモリに書き込まれていることです。以下の更新ログセクションを参照してください。事実上、複数のレコードを送信しようとしていますが、最後のレコードのみが取得されています。これは実際にはモデルに保存されていないため、他の問題が発生します。これは、NEW を呼び出すことによって引き起こされたようです。メソッドを CREATE に変更すると、リストの最後の項目が保存されます。ただし、フォームにリストされていないフィールドは nil として保存され、他の問題を引き起こしています。


コンソールから動作:

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

Contact モデルに 2 つの新しいレコードを作成します。だから私はフォームからそれを実現する方法を理解しようとしています。基本的。

独自のモデル内にフォームをネストできますか? 最初に連絡先をステージング テーブルに保存する必要がありますか? これを行う「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>

ログ:

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 を確認すると、実際には存在しませんが、アプリには存在します。

4

2 に答える 2

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>
于 2015-06-14T10:36:57.403 に答える