2017-03-10 2 views
0

좋은 밤 친구!check_boxes 오류가있는 simple_fields_for 오류

많이 쓰이는 양식에서 확인란과 필드 옆에 주어진 클래스 (도구)의 모든 개체를 표시해야합니다.

= simple_form_for @service, html: { class: 'form-horizontal' } do |f| 
    - @tools.each do |tool| 
     = f.simple_fields_for :instrumentalisations, tool do |i| 
     = i.input :tool_id, tool.id, as: :check_boxes 
     = i.input :amount 

그러나 나는 다음과 같은 오류를 받고 있어요 : 다음과 같이 내 형태는

Undefined method `tool_id 'for # <Tool: 0x007faef0327c28> 
Did you mean To_gid 

모델

class Service < ApplicationRecord 
    has_many :partitions, class_name: "Partition", foreign_key: "service_id" 
    has_many :steps, :through => :partitions 
    has_many :instrumentalisations 
    has_many :tools, :through => :instrumentalisations 

    accepts_nested_attributes_for :instrumentalisations 
end 

class Tool < ApplicationRecord 
    has_many :instrumentalisations 
    has_many :services, :through => :instrumentalisations 

    accepts_nested_attributes_for :services 
end 

class Instrumentalisation < ApplicationRecord 
    belongs_to :service 
    belongs_to :tool 
end 

컨트롤러

def new 
    @service = Service.new 
    @service.instrumentalisations.build 
end 

def edit 
    @tools = Tool.all 
end 

def create 
    @service = Service.new(service_params) 

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

def service_params 
    params.require(:service).permit(:name, :description, :price, :runtime, :status, step_ids: [], instrumentalisations_attributes: [ :id, :service_id, :tool_id, :amount ]) 
end 

감사합니다!

+0

외래 키인이 필드 : tool_id가 뭔가 방해를 받습니까? – wilfrank

+0

나는 눈치 내가 '= f.simple_fields_for에서 변경할 때 : instrumentalisations, 도구 할 | I |' 에 '= f.simple_fields_for : instrumentalisations가 할 | I |' 는 여러 도구 객체에 저를 소개합니다 , 중복 것처럼 보입니다 – wilfrank

답변

0

오류는 매우 간단합니다. 도구에는 tool_id 메서드가 없습니다. 하지만 도구 객체 대신 툴 객체를 요구하는 이유는 무엇입니까? 이 경우 :instrumentalisations이며, 제 2 인수는 record_object입니다 아 파크

f.simple_fields_for :instrumentalisations, **tool** do |i| 

fields_for가하는 record_name가 필요합니다

자, 어떤 instrumentalisations을 만들려고하지만 당신은 객체로 tool을 전달하는 어느 해야합니다 instrumentalisations 개체 및 tool 개체입니다.

따라서 수정하려면 instrumentalisation 개체를 전달해야합니다. 물론

f.simple_fields_for :instrumentalisations, Instrumentalisation.new(tool: tool) do |i| 

이이 객체가 새로운 instrumentalisations를 많이 만드는 것 편집하는 경우 이후 가장 좋은 방법이되지 않습니다 : 당신은에 의해 것을 수행 할 수 있습니다.

중첩 된 양식을보다 쉽게 ​​처리 할 수 ​​있도록 cocoon gem을 권장합니다.

+0

사실, 그게 내가 필요한 건 아니에요. 도구 클래스의 모든 개체를 표시하려면 체크 박스 필드와 항목 수를 추가하는 다른 텍스트 필드를 표시하고 싶습니다. 이 경우 고치 보석으로 내 문제가 해결되지 않습니다. [이 기사는 여기에 있습니다.] (http://millarian.com/rails/quick-tip-has_many-through-checkboxes/), 제가 찾고있는 것만 큼 가까운 것을 보여 드리지만, 하나 더 필드를 추가하고 [simple_form gem] (https://github.com/plataformatec/simple_form). – wilfrank

+0

공구 개체를 계측 루프에 전달하는 중 오류가 발생했습니다. 새로운 Instrumentalisation을 전달하기 위해 fields_for를 사용하는 것은 당신이하려는 일을 보관할 수있는 방법 중 하나 일뿐입니다. – dpedoneze

관련 문제