我的应用程序的基本概述。目前有两种型号。作业模型和客户模型。两种模型都有has_and_belongs_to_many
关系,因为我打算允许用户创建一个客户端条目,然后为他们分配一个或多个工作。
这是我的两个模型。
客户 -
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 应用程序,所以我不太确定如何。
任何帮助将不胜感激。