0

我用Rails 4.2和3.5制定并试图获得嵌套属性的工作设计(注册表格)(我已经通过多个职位消失了,似乎仍不能修复它。)Rails的嵌套属性与

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

模式

/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"=>"[email protected]", "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" = '[email protected]' LIMIT 1[0m 
    [1m[35mLead Exists (0.6ms)[0m SELECT 1 AS one FROM "leads" WHERE LOWER("leads"."email") = LOWER('[email protected]') 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", "[email protected]"], ["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_attributes)。

我的铬输出低于:

utf8:✓ 
authenticity_token:8EGTyqcpyWUdfcuWDtv0NB6sVa+i3aT9cQy5Iu/uV1Bnzs0ZNX0dNSsLzRFpuXhri1Zr7BglKAexkKuDOuFd8g== 
lead[first_name]:John 
lead[last_name]:Doe 
lead[phone_number]:3546545674 
lead[email]:[email protected] 
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 
+0

我没有任何直接的经验覆盖'Devise :: RegistrationsController',所以我不知道如何解决你的问题。但我会建议创建自己的'users_controller.rb',它具有':new'和':create'的动作。我们在应用程序中这样做,所以我们对整个请求 - 响应生命周期中发生的事情有全面的了解和控制。 –

+0

谢谢@ Jay-ArPolidario ...在我自己的'users_controller.rb'中将'resource.bookings.new'添加到':create',然后在下面的输出中创建下面的错误('“user_id不能为空”): 'bookings_attributes:!ruby/hash:ActionController ::参数 '0':!ruby/hash:ActionController ::参数 book_date:'2016-02-04T06:45:00 + 00:00' unit_type:One Bedroom agent_id:'1' 我怀疑它与在属性之前添加的'0'有关吗?思考?再次感谢 – Hughberto

+0

@Hughberto我的回答对你有帮助吗?请让我们知道,并考虑将我的答案标记为已接受,我将不胜感激。谢谢 – SsouLlesS

回答

0

有趣的是,它似乎错误是在我的预订模型中,我验证了lead_id,presence:true。删除它可以解决问题。感谢大家的帮助!

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: []