私のアプリの基本的な概要。現在2機種あります。ジョブ モデルとクライアント モデル。has_and_belongs_to_many
ユーザーがクライアント エントリを作成し、それらに 1 つまたは複数のジョブを割り当てられるようにすることを意図したため、両方のモデルには関係があります。
ここに私のモデルの両方があります。
クライアント -
class Client < ActiveRecord::Base
has_and_belongs_to_many :job
end
ジョブ -
class Job < ActiveRecord::Base
has_and_belongs_to_many :client
end
私はいくつかの調査を行ってきましたが、関係が機能するには外部キーが必要であると考えるのは正しいと思うので、データベースに列と列client_id
を追加しました。job_id
クライアントページは現在機能しており、これがそのためのコントローラーです。
class ClientsController < ApplicationController
def index
@clients = Client.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @clients }
end
end
# GET /Clients/1
# GET /Clients/1.json
def show
@clients = Client.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @clients }
end
end
# GET /Clients/new
# GET /Clients/new.json
def new
@clients = Client.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @clients }
end
end
# GET /Clients/1/edit
def edit
@clients = Client.find(params[:id])
end
def create
@clients = Client.new(params[:client])
respond_to do |format|
if @clients.save
format.html { redirect_to @clients, notice: 'Client was successfully created.' }
format.json { render json: @clients, status: :created, location: @clients }
else
format.html { render action: "new" }
format.json { render json: @clients.errors, status: :unprocessable_entity }
end
end
end
# PUT /Clients/1
# PUT /Clients/1.json
def update
@clients = Client.find(params[:id])
respond_to do |format|
if @clients.update_attributes(params[:client])
format.html { redirect_to @clients, notice: 'Client was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @clients.errors, status: :unprocessable_entity }
end
end
end
# DELETE /Clients/1
# DELETE /Clients/1.json
def destroy
@clients = Client.find(params[:id])
@clients.destroy
respond_to do |format|
format.html { redirect_to :clients , notice: 'Client was successfully removed.'}
format.json { head :no_content }
end
end
def details
@clients = Client.find_by_id(params[:id])
@jobs = Client.job
end
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])
@cients = Client.find = Client.find(params[:id])
respond_to do |format|
if @jobs.save
format.html { redirect_to(@jobs,
:notice => 'Job was successfully created.') }
format.xml { render :xml => @jobs,
:status => :created, :location => @Job }
else
format.html { render :action => "new" }
format.xml { render :xml => @jobs.errors,
:status => :unprocessable_entity }
end
end
end
end
私の求人フォームでは、作成されたすべてのクライアントのドロップダウンを追加する次のコードが与えられました。
<%= select("job", "client_id", Client.all.collect {|c| [ c.name, c.id ] }, {:include_blank => 'None'})%>
ただし、保存を押すと。次のエラーが表示されます。
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'
これは、ジョブの作成で client_id を見つける方法を定義し、クライアントの作成でそれを指定する必要があるためだと思います。
これは私の最初のRailsアプリですが、方法がよくわかりません。
どんな助けでも大歓迎です。