【问题标题】:Assigning an ID in Rails在 Rails 中分配 ID
【发布时间】:2014-08-01 18:56:52
【问题描述】:

我正在开发一个具有两个模型的测试应用程序 - 客户和工作。一个客户有_许多工作,一个工作属于一个客户。

在客户查看页面,有一个link_to create a New Job,如下:

<%= 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]

我在工作表上创建了 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

【问题讨论】:

  • 您想将customer_id 的自动分配值存储在jobs 中吗?
  • 发布您的jobs_controllernew.html.erb_form.html.erb
  • 您需要向我们展示添加新工作(不是客户)的视图页面代码
  • @RAJ 我的错,上面的表格已经相应地改变了。
  • 谢谢..不管怎样,看看我的回答,我认为可以解决你的问题

标签: ruby-on-rails routes


【解决方案1】:

您至少不需要JobAssignmentsController 来填充customer_id 字段。

您应该添加由cusmoters 包围jobs 的嵌套资源路由。在routes.rb中

resources :customers do
  resources :jobs
end

将链接修改为(rake routescustomer_id 交叉检查路径名称是此链接将为其创建工作的客户的 id):

<%= link_to "New Job", new_customer_job_path(customer_id) %> 

jobs_controller.rbnew 操作中,您应该利用关联。构建与customer 相关的job 对象。

添加

customer = Customer.find(params[:customer_id])
@job = customer.jobs.build

而不是

@job = Job.new

它将预先填充您的customer_id

阅读More

【讨论】:

  • 我收到 current_customer 的未定义局部变量或方法错误。请记住 - 用户登录并使用客户和工作填充应用程序。所以客户永远不会登录。我只想在客户页面上有一个链接来创建新工作并将客户 ID 拉入工作。
  • 非常接近 - 现在,当我单击新工作链接时,我收到一条错误消息:找不到 id=customer_id 的客户。所以现在它正在寻找 id,而不是把它拉下来。
  • 对不起,我应该提一下,您需要在link_to 中动态添加要为其创建工作的客户的ID 来代替customer_id
  • 我不知道该怎么做 - 你有我可以阅读的资源吗?
猜你喜欢
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多