Rails 4 Attempting to save Multiple Records to single Model -


i using omnicontacts gem pull list of contacts. far works. can contacts , use them populate form can pass them contacts controller saved in contact model.

i have looked @ rails cast #165 guidance , several posts here on stackoverflow. cannot seem crack nut. feels missing basic , driving me batty.


edit: issue restatement

the trouble having nil contact record getting written memory. please see update log section below. attempting submit multiple records, last being picked up. not getting saved model, causes other issues. appears caused calling new. changing method create saving last item in list. fields not listed in form saving nil causing other issues.


works console:

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

creates 2 new records in contact model. trying figure out how make happen form. basically.

can 1 nest form within own model? should save contacts staging table first? sees there should "rails" way of doing this. advice appreciated. view belongs import parts of controller.

controller:

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 |format|     format.html   end end 

view:

<div class="row">   <div class="small-12">     <%= simple_form_for contact.new |f| %>         <% unless @import.nil? %>             <% @import.each |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" ::1 @ 2015-06-14 06:38:52 -0500   user load (0.3ms)  select  "users".* "users" "users"."id" = $1  order "users"."id" asc limit 1  [["id", 1]] processing contactscontroller#create 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 "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 "users"."id" = $1  [["id", 1]]    (1.1ms)  commit redirected http://localhost:3000/contacts completed 302 found in 12ms (activerecord: 2.1ms)   started "/contacts" ::1 @ 2015-06-14 06:38:52 -0500   user load (0.2ms)  select  "users".* "users" "users"."id" = $1  order "users"."id" asc limit 1  [["id", 1]] processing contactscontroller#index html    (0.6ms)  select "contacts"."id" "contacts" "contacts"."user_id" = $1  [["user_id", 1]]   contact load (2.5ms)  select "contacts".* "contacts" "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".* "contacts" "contacts"."user_id" = $1  order "contacts"."last_name" asc, "contacts"."first_name" asc  [["user_id", 1]] 

when check table record 501 not exist, app does.

try changing view code this

<div class="row">     <div class="small-12">       <%= simple_form_for contact.new |f| %>         <% unless @import.nil? %>           <% @import.each |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> 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -