简体   繁体   English

如何更新模型记录? [路轨4]

[英]How to update a model record? [Rails 4]

I need to update my Pack model in my Rails application, but every time I submit the form, nothing happens. 我需要在Rails应用程序中更新Pack模型,但是每次提交表单时,都不会发生任何事情。 Here's the code: 这是代码:

packs_controller.rb packs_controller.rb

  def edit
    @pack = Pack.find(params[:id])
  end

  def update
    @pack = Pack.find(params[:id])
    if @pack.update_attributes(pack_params)
      flash[:success] = "Update successful!"
      redirect_to '/packs'
    else
      render 'edit'
    end
  end

  private

    def pack_params
      params.require(:pack).permit(:amount)
    end

_edit_packs.html.haml _edit_packs.html.haml

= form_for(@pack) do |f|
    .form-group
        = f.label :amount
        = f.text_field :amount, :autofocus => true, class: 'form-control'
    = f.submit 'Submit', :class => 'button right'

Any ideas? 有任何想法吗?

EDIT 编辑

Here is the log when pressing submit: 这是按下提交时的日志:

Started PATCH "/packs/11" for 127.0.0.1 at 2014-06-13 19:27:12 +0200
Started PATCH "/packs/11" for 127.0.0.1 at 2014-06-13 19:27:12 +0200
Processing by PacksController#update as HTML
Processing by PacksController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"oa20eUeZPxbV+mX0mQyDyFibwWCnOlrtP/9uoKq2HU0=", "pack"=>{"amount"=>"1"}, "commit"=>"Submit", "id"=>"11"}
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"oa20eUeZPxbV+mX0mQyDyFibwWCnOlrtP/9uoKq2HU0=", "pack"=>{"amount"=>"1"}, "commit"=>"Submit", "id"=>"11"}
  Pack Load (0.1ms)  SELECT  "packs".* FROM "packs"  WHERE "packs"."id" = ?  ORDER BY created_on DESC LIMIT 1  [["id", 11]]
  Pack Load (0.1ms)  SELECT  "packs".* FROM "packs"  WHERE "packs"."id" = ?  ORDER BY created_on DESC LIMIT 1  [["id", 11]]
   (0.1ms)  begin transaction
   (0.1ms)  begin transaction
  Pack Exists (0.1ms)  SELECT  1 AS one FROM "packs"  WHERE ("packs"."created_on" = '2014-06-13' AND "packs"."id" != 11) LIMIT 1
  Pack Exists (0.1ms)  SELECT  1 AS one FROM "packs"  WHERE ("packs"."created_on" = '2014-06-13' AND "packs"."id" != 11) LIMIT 1
   (0.0ms)  rollback transaction
   (0.0ms)  rollback transaction
  Rendered packs/_edit_packs.html.haml (1.8ms)
  Rendered packs/_edit_packs.html.haml (1.8ms)
  Rendered packs/edit.html.haml within layouts/application (2.5ms)
  Rendered packs/edit.html.haml within layouts/application (2.5ms)
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 3  ORDER BY "users"."id" ASC LIMIT 1
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 3  ORDER BY "users"."id" ASC LIMIT 1
  Rendered layouts/_navigation_links.html.erb (1.2ms)
  Rendered layouts/_navigation_links.html.erb (1.2ms)
  Rendered layouts/_navigation.html.haml (1.9ms)
  Rendered layouts/_navigation.html.haml (1.9ms)
  Rendered layouts/_messages.html.haml (0.1ms)
  Rendered layouts/_messages.html.haml (0.1ms)
Completed 200 OK in 28ms (Views: 22.0ms | ActiveRecord: 0.6ms)
Completed 200 OK in 28ms (Views: 22.0ms | ActiveRecord: 0.6ms)

EDIT 2 编辑2

After having looked through some of the comments, and thinking about the problem, it seems like there's a problem with my uniqueness validation on the :created_on field, which stores the date the record was created on. 在查看了一些评论并考虑了问题之后,似乎我在:created_on字段上的唯一性验证存在问题,该字段存储创建记录的日期。

EDIT 3 编辑3

Confirmed: :created_on is the root of the problem! 确认::created_on是问题的根源! I have a validation that enforces the uniqueness of :created_on. 我有一个验证,可以强制:created_on的唯一性。 When I remove it, the form works. 当我删除它时,该窗体有效。 Here is my model: 这是我的模型:

pack.rb pack.rb

class Pack < ActiveRecord::Base
    belongs_to :user

    default_scope -> { order('created_on DESC') }
    scope :today, -> { where(:created_at => (Time.now.beginning_of_day..Time.now)) }
    scope :week, -> { where(:created_at => (Time.now.beginning_of_week..Time.now))}
    scope :month, -> { where(:created_at => (Time.now.beginning_of_month..Time.now))}
    scope :year, -> { where(:created_at => (Time.now.beginning_of_year..Time.now))}

    validates :user_id, presence: true
    validates :created_on, uniqueness: true
    validates :amount, presence: true, length: { maximum: 1 }
end

Any suggestions on how to get around this? 关于如何解决这个问题的任何建议? Should I start a new question? 我应该开始一个新问题吗?

Based on your logs, it looks like you have multiple packs in your database that were created_on is equal to '2014-06-13' which will cause the created_on uniqueness validation to fail. 根据您的日志,看起来您的数据库中有多个packs, created_on等于'2014-06-13' ,这将导致created_on唯一性验证失败。

Check this in the rails console: 在rails控制台中检查以下内容:

Pack.where(created_on: '2014-06-13')

If there is more than one, delete the extras and it should work. 如果有多个,删除多余的部分,它应该可以工作。

如果您试图将一个包限制为每个用户每天一个,则需要在验证时添加一个范围选项:

validates :created_on, uniqueness: true, scope: user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM