0

我在为我的模型构造我的嵌套属性时遇到问题请求Rails 5 - 嵌套属性的强参数

数据以正确的方式从我的POST动作传递。

我缺少什么白名单这些参数?

感谢任何帮助。

控制台输出

Started POST "/requests" for 127.0.0.1 at 2017-06-08 10:57:15 -0400 
Processing by RequestsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"fmvhoPxVpcHoqOd32mO/HJMrfaPUd+KbNqDJiSRs78U44Y0uL3prpTfU6wmw7PAwv0b+mRHXOGMLvD9bsZpxnw==", "request"=>{"concierge_name"=>"Alex", "concierge_number"=>"954-123-4567", "concierge_email"=>"[email protected]", "client_name"=>"Adam", "client_number"=>"954-765-4321", "client_email"=>"[email protected]", "hotel_employee"=>"0", "concierge_service"=>"0", "vip_promoter"=>"0", "arriving_with_client"=>"1", "client_alone"=>"0", "males"=>"", "females"=>"1", "table_minimum"=>"1000", "arrival_time(1i)"=>"2017", "arrival_time(2i)"=>"6", "arrival_time(3i)"=>"8", "arrival_time(4i)"=>"14", "arrival_time(5i)"=>"56", "table_location_ids"=>["1"], "drink_attributes"=>[{"id"=>"1", "quantity"=>"1"}, {"id"=>"2", "quantity"=>""}, {"id"=>"3", "quantity"=>""}, {"id"=>"4", "quantity"=>""}], "chaser_ids"=>["1"], "comments"=>""}, "commit"=>"Submit"} 
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.0ms) 



ActiveModel::UnknownAttributeError (unknown attribute 'drink_attributes' for Request.): 

应用/模型/ request.rb

class Request < ApplicationRecord 
    has_many :request_drinks 
    has_many :drinks, through: :request_drinks 

    accepts_nested_attributes_for :drinks 
end 

应用程序/模型/ drink.rb

class Drink < ApplicationRecord 
    has_many :request_drinks 
    has_many :requests, through: :request_drinks 
end 

应用程序/模型/ request_drink.rb

class RequestDrink < ApplicationRecord 
    belongs_to :request 
    belongs_to :drink 
end 

应用程序/控制器/ request_controller.rb

class RequestsController < ApplicationController 
    before_action :set_request, only: [:show, 
            :edit, 
            :update, 
            :destroy] 

    def index 
    @requests = Request.search(params[:term], params[:filter], params[:page]) 
    end 

    def show 
    end 

    def new 
    @request = Request.new 
    @drinks = Drink.active 
    end 

    def edit 
    end 

    def create 
    @request = Request.new(request_params) 

    respond_to do |format| 
     if @request.save 
     format.html { redirect_to thanks_path, notice: 'Request was successfully created.' } 
     format.json { render :show, status: :created, location: @request } 
     else 
     format.html { render :new } 
     format.json { render json: @request.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @request.update(request_params) 
     format.html { redirect_to @request, notice: 'Request was successfully updated.' } 
     format.json { render :show, status: :ok, location: @request } 
     else 
     format.html { render :edit } 
     format.json { render json: @request.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @request.destroy 
    respond_to do |format| 
     format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 

    def set_request 
     @request = Request.find(params[:id]) 
    end 

    def request_params 
     params.require(:request).permit(:concierge_name, 
             :concierge_number, 
             :concierge_email, 
             :client_name, 
             :client_number, 
             :client_email, 
             :hotel_employee, 
             :concierge_service, 
             :vip_promoter, 
             :arriving_with_client, 
             :client_alone, 
             :people, 
             :males, 
             :females, 
             :table_minimum, 
             :arrival_time, 
             :comments, 
             :drink_attributes => [:id, :quantity] 
    ) 
    end 
end 

应用/视图/请求/ _form.html.erb

... 
    <div class="field-3"> 
     <h4>Drinks</h4> 
     <% @drinks.all.each do |d| %> 
     <%= hidden_field_tag "request[drink_attributes][][id]", d.id %> 
     <%= number_field_tag "request[drink_attributes][][quantity]" %> 
     <%= d.name %> 
     <br /> 
     <% end %> 
    </div> 
... 

回答

1

你需要使用对象的复数形式(即drinks)当使用嵌套属性

所以,在你request_params变化:

:drink_attributes => [:id, :quantity] 

到:

:drinks_attributes => [:id, :quantity] 

的需要也更新您的形式:

... 
    <%= hidden_field_tag "request[drinks_attributes][][id]", d.id %> 
    <%= number_field_tag "request[drinks_attributes][][quantity]" %> 
... 
+0

谢谢! - 这解决了最初的问题,但现在我收到此错误:**无法找到与ID = 1的ID = 1的饮料@ –

+0

@AlexanderSmith请您发布完整的错误日志? – Gerry

+0

@AlexanderSmith如果可能的话发布一个具有该特定错误的新问题,所以这个问题的范围保持不变。 – Gerry