【问题标题】:Updating an existing entry in my database isn't working更新我的数据库中的现有条目不起作用
【发布时间】:2012-05-20 21:41:45
【问题描述】:

我正在尝试在名为 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

我想得到这个工作是一个相对容易的修复,但这是我的 forst 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

【问题讨论】:

  • 需要有关您的表单的更多信息
  • 将我的工作 _form 添加到第一个帖子
  • 您是否也可以使用作业模型源代码更新您的问题?你使用 attr_accessible 吗?
  • 完成。 attr_accessible 方法有什么作用?
  • attr_accessible 指定可以通过批量分配设置的模型属性白名单。 bit.ly/KhZRC6 但我认为不是这种情况,因为您没有质量分配错误。

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


【解决方案1】:

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

而且您的控制器中似乎缺少 edit 操作。

【讨论】:

  • 编辑现在工作正常。但是,当我现在创建一个新工作时。除了我指定的客户详细信息外,名称等所有详细信息都在保存。在成功屏幕上出现空白。
  • 您的关联有误,将belongs_to :clients 替换为belongs_to :client
  • 取得进展 :-) 我现在成功地创建了这个工作,并且列出了客户端.. 但看起来它是以散列而不是文本值的形式出现的。 Job was successfully created. Job Name: Weekly test Client: **#&lt;Client:0x000001018b08b0&gt;** Frequency: Never Owner: Me
  • 我认为更新不是问题......但如果你的意思是这个#&lt;Client:0x000001018b08b0&gt; 它是一个客户端对象,我认为没关系。
  • 文本值是什么?如果客户端有名称,您可以在该对象上调用名称方法client.name
猜你喜欢
  • 2021-11-25
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 1970-01-01
  • 2021-08-30
相关资源
最近更新 更多