0

我试图使用我的客户端模型中创建的名为Jobs的新模型中创建的客户端列表。更新我的数据库中的现有条目不起作用

基本上。用户应该能够查看当前分配给任何一个客户端的作业列表,然后深入查看更多信息。

我已经插入一个新列到我的工作中叫CLIENT_ID并插入下面的代码在我_form视图可以看到一个下拉的所有客户端

<%= f.label :client_id %><br /> 
<%= f.collection_select :client_id, @clients, :id, :name, :prompt => 
    "Select" %> 

然而列表。当我点击提交时,它会尝试将其张贴到jobs/new,这根据我的资源路线。不存在。

我也在数据库中插入了一些虚拟数据,尽管当我尝试编辑它时显示得很好。按保存不会对记录做任何事情。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"0ZVYpM9vTgY+BI55Y9yJDwCJwrwSgGL9xjHq8dz5OBE=", "job"=>{"name"=>"Sample Monthly", "client_id"=>"2", "frequency"=>"Monthly", "owner"=>"Me"}, "commit"=>"Save Job", "id"=>"1"} 
    Job Load (0.3ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = ? LIMIT 1 [["id", "1"]] 
    (0.1ms) begin transaction 
    (0.5ms) UPDATE "jobs" SET "name" = 'Sample Monthly', "frequency" = 'Monthly', "updated_at" = '2012-05-12 17:04:23.818967' WHERE "jobs"."id" = 1 
    (108.3ms) commit transaction 
Redirected to http://localhost:3000/jobs/1 

这里是我的控制器..

class JobsController < ApplicationController 
    before_filter :load_clients, :only => [ :new, :create, :edit ] 

    def index 
    @job = Job.find(:all) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @job } 
    end 
    end 

    def new 
    @job = Job.new 
    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @Job } 
    end 
    end 

    def create 
    @job = Job.new(params[:job]) 

    respond_to do |format| 
     if @job.save 
     format.html { redirect_to @job, notice: 'Job was successfully created.' } 
     format.json { render json: @job, status: :created, location: @job } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @job.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def show 
    @job = Job.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @job } 
    end 
    end 

    def update 
    @job = Job.find(params[:id]) 

    respond_to do |format| 
     if @job.update_attributes(params[:job]) 
     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 

    private 
    def load_clients 
     @client = Client.find(:all) 
    end 
    end 

我认为得到这个工作是一个相对简单的解决方法,但是这是我的抗冻Rails应用程序,我不知道从哪里开始。

编辑:

根据要求。这是我的工作表格:

<%= form_for(:job) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :client_id %><br /> 
    <%= f.collection_select :client_id, @client, :id, :name, :prompt => 
    "Select" %> 
    </div> 
    <div class="field"> 
    <%= f.label :frequency %><br /> 
    <%= f.text_field :frequency %> 
    </div> 
    <div class="field"> 
    <%= f.label :owner %><br /> 
    <%= f.text_field :owner %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

编辑2:这是我的工作模型。

class Job < ActiveRecord::Base 
    belongs_to :clients 
end 
+0

需要关于您的表格的更多信息 – Suborx

+0

添加我的工作_form到第一篇文章 – Keva161

+0

您还可以使用Job模型源代码更新您的问题吗?你使用attr_accessible吗? – cutalion

回答

1

尝试在您的表单中将:job替换为@job

而且看起来你在控制器中缺少edit动作。

+0

编辑现在正常工作。但是,当我现在创造一份新工作。所有的细节,如名称等都保存,除了客户端的细节,虽然我指定。成功屏幕上空白。 – Keva161

+0

你有错误的关联替换'belongs_to:客户端'与'belongs_to:客户端' – Suborx

+0

正在取得进展:-) 我现在成功地创建了第四个工作,客户端被列出..但它看起来像是作为一个散列而不是文本值。 '作业已成功创建。 工作 名称: 每周测试 客户: **#<客户端:0x000001018b08b0> ** 频率: 决不 业主: Me' – Keva161

相关问题