Rails嵌套属性与设计(注册表单)

时间:2016-02-01 21:45:38

标签: ruby-on-rails ruby ruby-on-rails-4 devise nested-attributes

我正在使用Rails 4.2和Devise 3.5并尝试使嵌套属性正常工作(我已经浏览了多个帖子但仍然无法修复它。)

实际上,我有一个潜在客户模型和一个预订模型(潜在客户可以有多个预订)。

模型

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

控制器(非常确定这是错误的)

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

查看

  

    

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

记录输出

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)

不知道我哪里出错...日志说unpermitted_perimeter:booking(但应该是booking_attributes)。

我的chrome输出低于:

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 

2 个答案:

答案 0 :(得分:0)

您的表单发送参数的方式是仅向用户附加1次预订,而不是“很多”,这是您的预订达到设计的方式:

"booking"=>{"book_date"=>"2016-02-11T09:15:00+00:00", "unit_type"=>"Two Bedroom", "agent_id"=>"1"}

这是因为在您的表单中您使用的是单数符号:

  #note you are using :booking instead of :bookings that's why in your params it outputs "booking" instead of "booking_attributes"
  <%= f.fields_for :booking 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 %>

如果您只想附上1张预订,请使用以下内容:

before_action :configure_permitted_parameters #,if: :devise_controller? I think that if: is unnecesary.

 protected

 def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u|
     u.permit(:email, :password, :first_name, :last_name, :phone_number, :birthday, booking: [:unit_type, :book_date, :agent_id])
}
 end

然后,如果您想允许多个预订,请在<%= f.fields_for :bookings do |booking_form| %>

中添加复数

然后您可以在permit语句中使用booking_attributes: []

答案 1 :(得分:0)

有趣的是 - 似乎错误出现在我的预订模型中,我在验证lead_id,存在:是的。删除此功能可解决此问题。感谢大家的帮助!