我的應用程序的基本概述。目前有兩種型號。一個工作模型和一個客戶模型。這兩種型號都有has_and_belongs_to_many
關係,因爲我打算允許用戶創建客戶端條目,然後爲其分配一個或多個作業。如何自動爲條目分配一個唯一的ID用於關係?
這是我的兩個模型。
客戶 -
class Client < ActiveRecord::Base
has_and_belongs_to_many :job
end
工作 -
class Job < ActiveRecord::Base
has_and_belongs_to_many :client
end
我一直在做一些研究,我認爲在想,這種關係需要一個外鍵功能,從而增加了一個client_id
列IM權& a 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
在我的工作形成我THR以下這增加了下拉與所有的客戶創建代碼中給出。
<%= 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應用程序,雖然我不太清楚如何。
任何幫助將不勝感激。
謝謝你的建議。這是我第一次Rails應用程序/嘗試在'後端'編碼,所以原諒任何錯誤:) 唯一的問題。我想我必須從聯結表分配一個客戶IT? 我該怎麼做? – Keva161
您不必以任何方式與交接表交互; Rails會爲你管理它。您只需將一個客戶端分配給一個工作('@job.clients << @ client')或將一個工作分配給客戶端('@client.job = @ job')。 – meagar
當我嘗試創建一個工作,即時獲取'未知屬性:client_id app/controllers/jobs_controller.rb:23:在'new' app/controllers/jobs_controller.rb:23:在'create'' – Keva161