私は2つのモデルjobs
とを持っていclients
ます。
ユーザーは、クライアントを簡単に作成してから、それらにいくつかのジョブを割り当てることができます。
これが両方の私のモデルです。
job.rb
class Job < ActiveRecord::Base
has_and_belongs_to_many :clients
end
client.rb
class Client < ActiveRecord::Base
has_and_belongs_to_many :jobs
end
新しいジョブを作成するための私のフォームは次のようになります。
<%= simple_form_for :job do |f| %>
<%= f.input :name %>
<%= <%= collection_select(:job, :client_ids, Client.all, :id, :name, {:include_blank => 'None'}, { :multiple => true }) %>%>
<%= f.button :submit %>
<% 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"}
私のジョブコントローラーは非常に基本的で、次のようになります。
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
との両方job_id
をclient_id
整数値として使用するジャンクションテーブルを設定しています。したがって、エラーメッセージが示すように、新しいアクションと作成アクションの下でコントローラーでそれらを定義する場合にすぎないと思います。
これは私の最初のRailsアプリですが、方法はよくわかりません。
どんな助けでも大歓迎です!