我正在开发一个有两个模型的测试应用程序 - 客户和工作。客户有很多工作,而工作属于客户。
在客户视图页面上,有一个link_to创建一个新工作,如下所示:
<%= link_to "New Job", new_job_path %>
当用户在客户页面上单击此新作业链接时,我希望从客户视图页面将作业分配给相应的客户ID。
在寻找答案时,我发现了这一点:What's the best practice for these kinds of routes?
所以我设置了一个单独的控制器,将客户ID分配给作业,如下所示(job_assignments_controller.rb):
class JobAssignmentsController < ApplicationController
def create
customer = Customer.find(params[:customer_id])
job = Job.find(params[:job_id])
customer.assign(job, params[:job_type])
end
def destroy
customer = Customer.find(params[:customer_id])
job = Job.find(params[:job_id])
customer.unassign(job, params[:job_type])
end
end
我已按如下方式设置资源路径:
resources :job_assignments, :only => [:create]
我在jobs表上创建了customer_id作为外键,并且可以使用Rails控制台手动将作业分配给客户ID。
我不知道如何从这里开始实现我的目标(当从客户视图页面点击新的工作链接时,将客户ID自动分配给工作ID)。
关于我是否走上正轨或我缺少什么的任何想法或想法?
附录:
_form.html.erb:
<%= form_for(@job) do |f| %>
<% if @job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% @job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :installation %><br>
<%= f.text_field :installation %>
</div>
<div class="field">
<%= f.label :install_date %><br>
<%= f.text_field :install_date %>
</div>
<div class="field">
<%= f.label :delivery %><br>
<%= f.text_field :delivery %>
</div>
<div class="field">
<%= f.label :box_count %><br>
<%= f.number_field :box_count %>
</div>
<div class="field">
<%= f.label :room_type %><br>
<%= f.text_field :room_type %>
</div>
<div class="field">
<%= f.label :material %><br>
<%= f.text_field :material %>
</div>
<div class="field">
<%= f.label :exterior_colour %><br>
<%= f.text_field :exterior_colour %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
附录2:
jobs_controller:
class JobsController < ApplicationController
before_action :set_job, only: [:show, :edit, :update, :destroy]
def index
@jobs = Job.all
end
def show
end
def new
@job = Job.new
end
def edit
end
def create
@job = Job.new(job_params)
respond_to do |format|
if @job.save
format.html { redirect_to @job, notice: 'Job was successfully created.' }
format.json { render action: 'show', status: :created, location: @job }
else
format.html { render action: 'new' }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @job.update(job_params)
format.html { redirect_to @job, notice: 'Job was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
def destroy
@job.destroy
respond_to do |format|
format.html { redirect_to jobs_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_job
@job = Job.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def job_params
params.require(:job).permit(:job_number, :installation, :install_date, :delivery, :box_count, :room_type, :material, :exterior_colour)
end
end
答案 0 :(得分:2)
您至少不需要JobAssignmentsController
来填充customer_id
字段。
您应该添加jobs
cusmoters
之后的嵌套资源路由。在routes.rb
resources :customers do
resources :jobs
end
将链接修改为(rake routes
的交叉检查路径名称和此链接将为其创建作业的客户的customer_id
为id
):
<%= link_to "New Job", new_customer_job_path(customer_id) %>
在new
的{{1}}行动中,你应该从协会中受益。与jobs_controller.rb
建立job
对象。
添加
customer
而不是
customer = Customer.find(params[:customer_id])
@job = customer.jobs.build
它会预先填充您的@job = Job.new
。
阅读More