Rails 4:Update_attributes不起作用,但update_column不起作用

时间:2014-04-29 22:57:53

标签: ruby-on-rails postgresql ruby-on-rails-4 postgresql-9.2

目前我有一个编辑表格如下:

<li>
    <%= form_for @ingredient do |f| %>
        <span class="span2"><%= f.text_field :name, placeholder: "#{@ingredient.name}" %></span>
        <span class="span1"><%= f.text_field :quantity, placeholder: "#{@ingredient.quantity}" %></span>
        <span class="span1"><%= f.text_field :unit, placeholder: "#{@ingredient.unit}" %></span>
        <span class="span3">Added: <%= @ingredient.updated_at.strftime("%d %b. at %H:%M") %></span>
        <span class="span2"><%= f.text_field :expiration, placeholder: "#{@ingredient.expiration}" %></span>
        <span class="span2"><%= f.submit "Submit", class: "btn btn-small" %></span>
    <% end %>
</li>

当我点击提交时,我的日志文件显示如下:

Started PATCH "/pantries/112" for 127.0.0.1 at 2014-04-29 18:03:35 -0400
Processing by PantriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"NUSmadjWCVVLHOZmncKD5D48L+7ZMa3DEbZ9Y+Y+Pnc=", "pantry"=>{"name"=>"test1", "quantity"=>"1", "unit"=>"cup", "expiration"=>"2015-05-05"}, "commit"=>"Submit", "id"=>"112"}
  [1m[35mUser Load (0.5ms)[0m  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '27ecdc04fc67375fd3567c89fbe831e4d4919d09' LIMIT 1
  [1m[36mPantry Load (0.3ms)[0m  [1mSELECT "pantries".* FROM "pantries" WHERE "pantries"."id" = $1 LIMIT 1[0m  [["id", "112"]]
  [1m[35m (0.2ms)[0m  BEGIN
  [1m[36m (0.1ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/users/1/pantry
Completed 302 Found in 6ms (ActiveRecord: 1.1ms)

它不会引发错误,它根本不会更新,但表示更新已成功完成。

pantry.rb

class Pantry < ActiveRecord::Base
    before_save { self.name = name.downcase }
    belongs_to :user

    validates :name, presence: true
    validates :user_id, presence: true
end

pantries_controller

def update
        @ingredient = Pantry.find(params[:id])
        if @ingredient.update_attributes(params[ingredient_params])
            redirect_to pantry_user_path(current_user), :flash => {info: "Ingredient Updated"}
        else
            redirect_to pantry_user_path(current_user), :flash => {info: "Failed"}
        end
end
private
        def ingredient_params
            params.require(:pantry).permit(:name, :quantity, :unit, :expiration, :created_at, :updated_at)
        end

架构:

create_table "pantries", force: true do |t|
    t.string   "name"
    t.string   "quantity"
    t.string   "unit"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.date     "expiration"
  end

  add_index "pantries", ["expiration"], name: "index_pantries_on_expiration", using: :btree
  add_index "pantries", ["quantity"], name: "index_pantries_on_quantity", using: :btree
  add_index "pantries", ["unit"], name: "index_pantries_on_unit", using: :btree

如果我将@ingredients.update_attributes替换为@ingredient.update_column(:expiration, params[:pantry][:expiration]),则会在该列上进行更新。回到update_column并不理想。我理解update_attributesupdate_attributes!调用回调,而update_column则不然。我没有看到回调的任何问题,也没有给出错误消息。任何人都知道问题可能是什么?

1 个答案:

答案 0 :(得分:1)

更改update操作,如下所示:

def update
        @ingredient = Pantry.find(params[:id])
        if @ingredient.update_attributes(ingredient_params) ## Call ingredient_params
            redirect_to pantry_user_path(current_user), :flash => {info: "Ingredient Updated"}
        else
            redirect_to pantry_user_path(current_user), :flash => {info: "Failed"}
        end
end

使用Rails 4 强参数概念,您需要将要保存在数据库中的属性列入白名单。

目前,您使用params[ingredient_params]而不是调用导致此问题的ingredient_params