1

내 응용 프로그램에 대한 간략한 개요입니다.두 모델을 함께 연결할 때 알 수없는 특성

처음에는 사용자가 한 페이지에 클라이언트 세트 A를 작성한 다음 다른 페이지를 작성하여 사용자에게 작업을 할당하고 할당합니다.

내 클라이언트 모델 및보기가 예상대로 작동하지만 내 작업 모델을 연결할 수 없습니다.

여기 내 직업 모델입니다.

class Client < ActiveRecord::Base 
    has_and_belongs_to_many :jobs 
end 

class Job < ActiveRecord::Base 
    has_and_belongs_to_many :clients 
end 

여기 내 고객 컨트롤러도 있습니다.

class JobsController < ApplicationController 

    def index 
    @jobs = Job.find(:all) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @job } 
    end 
    end 

    def new 
    @jobs = Job.new 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @job } 
    end 
    end 

    def create 
    @jobs = Job.new(params[:job]) 
    respond_to do |format| 
     if @jobs.save 
     format.html { redirect_to @jobs, notice: 'Job was successfully created.' } 
     format.json { render json: @jobs, status: :created, location: @jobs } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @jobs.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def show 
    @jobs = Job.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @jobs } 
    end 
    end 
end 

제 양식에는 두 개의 필드가 있습니다. 하나는 작업 이름이고 다른 하나는 데이터베이스에 나열된 모든 클라이언트에서 드롭 다운입니다.

그러나 이것을 기입하면 다음 오류 메시지가 나타납니다.

ActiveRecord::UnknownAttributeError in JobsController#create 

**unknown attribute: client_id** 

Application Trace | Framework Trace | Full Trace 
app/controllers/jobs_controller.rb:22:in `new' 
app/controllers/jobs_controller.rb:22:in `create' 
Request 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"0ZVYpM9vTgY+BI55Y9yJDwCJwrwSgGL9xjHq8dz5OBE=", 
"job"=>{"name"=>"Sample Monthly", 
"client_id"=>"1"}, 
"commit"=>"Save Job"} 

나는 내가 어디 client_id

를 선언 할 필요가 가정

class AddClientsJobsTable < ActiveRecord::Migration 
    def up 
    create_table :clients_jobs, :id => false do |t| 
     t.belongs_to :job, :client 
     t.integer :client_id 
     t.integer :job_id 
    end 
end 

    def down 
    drop_table :clients_jobs 
    end 
end 

.. 접합 테이블 설치도 clients_jobs라고해야하지만, 이것은 내 첫 번째 레일 응용 프로그램과 메신저 확실하지 않다.

도움을 주시면 감사하겠습니다.

편집 : 내 직업의 양식입니다. A 클라이언트 HABTM 협회,하지만 작업 (한) 클라이언트에 속하는 것처럼 폼을 구현 -

<%= simple_form_for :job do |f| %> 
    <%= f.input :name %> 
    <%= select("job", "client_id", Client.all.collect {|c| [ c.name, c.id ] }, {:include_blank => 'None'})%> 
    <%= f.button :submit %> 
<% end %> 
+0

작업 양식에 대한 코드도 추가 할 수 있습니까? – Adam

+0

첫 번째 게시물을 양식 코드로 편집했습니다. – Keva161

답변

0

귀하의 모델은 작업 상태. 입력에서

<%= collection_select(:job, :client_ids, Client.all, :id, :name, {:include_blank => 'None'}, { :multiple => true }) %> 

노트 복수 'client_ids'및 수 있도록 여러 : 당신의 의도가 여러 클라이언트에 작업을 할당 할 수있을 것입니다 실제로 경우에 대한이 같은 것을 보일 것입니다.

작업이 한 명의 사용자에게만 속한 경우 has_and_belongs_to_many : clients를 사용하지 않아야합니다.

+0

클라이언트는 많은 작업을 할 수 있지만 예, 고유 한 작업은 하나의 클라이언트에만 속할 수 있습니다.이 텍스트는 여전히 'unknown attribute : client_id'를 수신합니다. – Keva161