2017-04-11 132 views
0

我張貼這些PARAMS形式:不能得到params.permit工作

{"utf8"=>"✓", "authenticity_token"=>"XXXXX", "bike_part_id"=>["367", "368"], 
"commit"=>"registrer en [48/1854] 
"controller"=>"services", "action"=>"create", "bike_id"=>"10"} 

這是控制器代碼:

def create 
    @service = Service.new(service_params) 
    @service.bike = @bike 
    @service.bike_parts = @bike_parts 

    respond_to do |format| 
     if @service.save! 
     #[cut..] 
end 

-

def service_params 
     params.permit(:description, :log, :due_date, :bike_id, 
        :user_id, :completed, :bike_part_id 
        ) 
    end 

但我不斷收到這個日誌:

Unpermitted parameters: utf8, authenticity_token, bike_part_id, commit 

爲什麼不允許bike_part_id?

+1

你可以發表你的表格代碼嗎? – hoangdd

回答

0

對於數組,您需要爲強參數指定數組是否應爲預期值。

def service_params 
     params.permit(:description, :log, :due_date, :bike_id, 
        :user_id, :completed, bike_part_id: [] 
        ) 
end 
1

因爲它是嵌套的。請嘗試以下操作:

def service_params 
    params.permit(
    :description, :log, :due_date, :bike_id, user_id, :completed, bike_part_id: [] 
) 
end