I am using Rails 4.2 and Devise 3.5 and trying to get nested attributes working (i've gone through multiple posts and still can't seem to fix it.)
Effectively I have a leads model and a bookings model (where a lead can have multiple bookings).
Models
/lead.rb
class Lead < ActiveRecord::Base
has_many :bookings, autosave: true
accepts_nested_attributes_for :bookings
..
/booking.rb
class Booking < ActiveRecord::Base
belongs_to :lead
belongs_to :agent
..
Controller (pretty sure this is wrong)
class MyDevise::RegistrationsController < Devise::RegistrationsController
def new
build_resource({})
resource.bookings.build
respond_with self.resource
end
end
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :first_name, :last_name, :phone_number, :birthday, [bookings_attributes: [:unit_type, :book_date, :agent_id]])
}
end
View
<div class="row">
<div class="col-md-6" id="left">
<div class="field">
<%= f.text_field :first_name, autofocus: true, :placeholder => "First Name", class: 'form-control' %>
</div>
</div>
<div class="col-md-6" id="right">
<div class="field">
<%= f.text_field :last_name, autofocus: true, :placeholder => "Last Name", class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="field">
<%= f.text_field :phone_number, autofocus: true, :maxlength => 10, :placeholder => "Phone Number", class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="field">
<%= f.email_field :email, autofocus: true, :placeholder => "Email", class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="field">
<%= f.password_field :password, autocomplete: "off", :placeholder => "Password (8 characters minimum)", class: 'form-control' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="dropdown">
<%= f.label :birthday, :display => 'block', :id => 'signup' %>
<%= f.date_select :birthday, {:start_year => 1910, order: [:month, :day, :year] }, {:class => 'btn btn-default dropdown-toggle', :display => 'block'} %>
</div>
<a href="">Why do we need your birthdate?</a>
</div>
</div>
<%= f.fields_for :bookings do |booking_form| %>
<%= booking_form.hidden_field :book_date, :value => time_helper %>
<%= booking_form.hidden_field :unit_type, :value => session[:unit_type] %>
<%= booking_form.hidden_field :agent_id, :value => 1 %>
<% end %>
<div class="row">
<div class="col-md-12">
<div class="actions">
<%= f.submit "Complete Booking", class: "btn btn-lg btn-primary" %>
</div>
</div>
</div>
<% end %>
Log output
Started POST "/leads" for 172.17.0.1 at 2016-02-01 20:53:54 +0000
Cannot render console from 172.17.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by MyDevise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"chXEuFe8xLBAHT6YS/QwJvmVOwWDvt47Or5Re8kzzvblmpprxegQ4HZrOB8slrx5bG8FRjlGUsH6IkPaHDzEVA==", "lead"=>{"first_name"=>"Hugh", "last_name"=>"Kolias", "phone_number"=>"4169929038", "email"=>"hughkolias@gmail.comaaaaaaaaaaaaaaaaaaaaaaaaf", "password"=>"[FILTERED]", "birthday(2i)"=>"2", "birthday(3i)"=>"1", "birthday(1i)"=>"2016", "booking"=>{"book_date"=>"2016-02-11T09:15:00+00:00", "unit_type"=>"Two Bedroom", "agent_id"=>"1"}}, "commit"=>"Complete Booking"}
Unpermitted parameter: booking
[1m[35m (0.2ms)[0m BEGIN
[1m[36mLead Exists (0.7ms)[0m [1mSELECT 1 AS one FROM "leads" WHERE "leads"."email" = 'hughkolias@gmail.comaaaaaaaaaaaaaaaaaaaaaaaaf' LIMIT 1[0m
[1m[35mLead Exists (0.6ms)[0m SELECT 1 AS one FROM "leads" WHERE LOWER("leads"."email") = LOWER('hughkolias@gmail.comaaaaaaaaaaaaaaaaaaaaaaaaf') LIMIT 1
[1m[36mLead Load (0.5ms)[0m [1mSELECT "leads".* FROM "leads" WHERE "leads"."confirmation_token" = $1 ORDER BY "leads"."id" ASC LIMIT 1[0m [["confirmation_token", "ab4a85d7cb85aeb981ebb6f7629a4d622f17289ff5ec9d88df18530be136d5a6"]]
[1m[35mSQL (0.4ms)[0m INSERT INTO "leads" ("email", "encrypted_password", "first_name", "last_name", "phone_number", "birthday", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["email", "hughkolias@gmail.comaaaaaaaaaaaaaaaaaaaaaaaaf"], ["encrypted_password", "$2a$10$mIk06TLAJFB9EG.emLkoeO5FQ2ckBiJHBIsE16FX/8dx57kcmezsu"], ["first_name", "Hugh"], ["last_name", "Kolias"], ["phone_number", "4169929038"], ["birthday", "2016-02-01"], ["created_at", "2016-02-01 20:53:54.564453"], ["updated_at", "2016-02-01 20:53:54.564453"], ["confirmation_token", "8zxs-PG8TLDByFoyFGMr"], ["confirmation_sent_at", "2016-02-01 20:53:54.567150"]]
Rendered devise/mailer/confirmation_instructions.html.erb (3.0ms)
Not sure where i'm going wrong... the log says unpermitted_perimeter: booking (but it should be booking_attributes).
My chrome output is below:
utf8:✓
authenticity_token:8EGTyqcpyWUdfcuWDtv0NB6sVa+i3aT9cQy5Iu/uV1Bnzs0ZNX0dNSsLzRFpuXhri1Zr7BglKAexkKuDOuFd8g==
lead[first_name]:John
lead[last_name]:Doe
lead[phone_number]:3546545674
lead[email]:johndoe@fsdafsadgmail.comfsdfsdafdsa
lead[password]:fsdafasfdsafsadfasd
lead[birthday(2i)]:2
lead[birthday(3i)]:1
lead[birthday(1i)]:2016
lead[booking][book_date]:2016-02-10T06:00:00+00:00
lead[booking][unit_type]:One Bedroom
lead[booking][agent_id]:1
commit:Complete Booking
Ah interesting - it seems the error was in my bookings model where I was validating lead_id, presence: true. Removing this fixes the issue. Thanks for the help everyone!