更新数据库中的现有条目无效

时间:2012-05-12 17:12:19

标签: ruby-on-rails ruby ruby-on-rails-3.2 foreign-keys database-relations

我正在尝试在我的新模型中使用从我的客户端模型创建的客户端列表。

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

我在我的job数据库中插入了一个名为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 %>

Edit2:这是我的工作模式。

class Job < ActiveRecord::Base
  belongs_to :clients
end

1 个答案:

答案 0 :(得分:1)

尝试使用表单中的:job替换@job

看起来你的控制器中缺少edit动作。