14

Tôi đọc khoảng collection_check_boxes nhưng tôi không hiểu làm cách nào tôi có thể đặt các giá trị đã chọn. tôi có mô hình sau:Các thông số chưa được truyền trong đường ray 4

class Objective < ActiveRecord::Base 

    has_many :indicators 
    has_many :objective_children, class_name: "Objective", foreign_key: "parent_id" 

    def objective_ids 
    objective_children.collect{|o| o.id} 
    end 

    def objective_ids= objectives_ids 
    objectives_ids.each do |id| 
     objective_children << Objective.find(id) 
    end 
    end 
end 

chỉnh sửa xem:

<%= form_for(@objective) do |f| %> 
    <%= f.collection_check_boxes :objective_ids, Objective.all, :id, :name %> 
    <%= f.submit %> 
<% end %> 

hộp kiểm html là ok nhưng tôi không biết làm thế nào để thiết lập các giá trị để objective. Tôi đã cố gắng xác định objective_ids= objectives_ids nhưng không có gì xảy ra.

Trong Bộ điều khiển:

class ObjectivesController < ApplicationController 
    def objective_params 
     params.require(:objective).permit(:name, :code, :description, :objective_ids) 
    end 
end 

EDIT Các file log nói Unpermitted parameters: perspective_id, objective_ids

Trả lời

25

tôi đã giải quyết việc thay đổi dòng

params.require(:objective).permit(:name, :code, :description, :objective_ids) 

để

params.require(:objective).permit(:name, :code, :description, :objective_ids => []) 
+0

Nó cũng có thể được viết dưới dạng '.permit (: name,: code,: description, object_ids: [])' – user664833

3

tôi nhận được cùng một vấn đề, hãy thử dòng này:

params.require(:objective).permit(:name, :code, :description, :objective_ids => []) 

nhưng không làm việc và tôi thay đổi để:

params.require(:objective).permit(:name, :code, :description, {:objective_ids => []}) 

và nó hoạt động !!