rails控制器不将值保存到数据库中的boolen字段

时间:2016-06-09 23:13:46

标签: ruby-on-rails ruby

我有一个带控制器的模型商店。该控制器有一个动作“发布”,用于设置store:publish字段为true或false。

def publish
    @store=Store.find_by(params[:store_id])
    if params[:publish_value]=="1"
        if @store.update_attribute :publish, true
            @message_type="success"
            @message="Your store has been published succesfully."
            respond_to do |format|
                format.js
            end
        else
            @message_type = "error"
            @message = @store.errors.full_messages.join(', ')
            respond_to do |format|
                format.js { render :template => "layouts/messages.js.erb", 
                             :layout => false }
            end
        end
    else
        if @store.update_attribute :publish, false
            @message_type="warning"
            @message="Your store has been unpublished."
            respond_to do |format|
                format.js
            end
        else
            @message_type = "error"
            @message = @store.errors.full_messages.join(', ')
            respond_to do |format|
                format.js { render :template => "layouts/messages.js.erb", 
                             :layout => false }
            end
        end
    end
end

当我发布商店时,params [:store_id]和params [:publish_value]成功传递给控制器​​。我收到“成功”消息。但由于某种原因,当我检查rails控制台或刷新页面时,发布值没有变化。它仍显示为零。

点击发布按钮后的日志。

Started POST "/stores/publish" for 39.32.68.180 at 2016-06-09 23:08:28 +0000
Processing by StoresController#publish as JS
Parameters: {"utf8"=>"✓", "store_id"=>"35", "publish_value"=>"1", "commit"=>"submit"}
Customer Load (0.2ms)  SELECT  "customers".* FROM "customers" WHERE "customers"."id" = ? LIMIT 1  [["id", 44]]
Store Load (0.1ms)  SELECT  "stores".* FROM "stores" WHERE "stores"."customer_id" = ? LIMIT 1  [["customer_id", 44]]
Store Load (0.3ms)  SELECT  "stores".* FROM "stores" WHERE (35) LIMIT 1
(0.1ms)  begin transaction
(0.1ms)  commit transaction
Rendered stores/_publish.html.erb (0.6ms)
Rendered layouts/_messages.js.erb (0.1ms)
Rendered stores/publish.js.erb (4.5ms)
Completed 200 OK in 14ms (Views: 8.9ms | ActiveRecord: 0.7ms)

帮助将不胜感激。感谢。

此处还有架构

create_table "stores", force: :cascade do |t|
t.string   "store_name"
t.integer  "customer_id"
t.text     "store_description"
t.datetime "created_at",        null: false
t.datetime "updated_at",        null: false
t.string   "picture"
t.boolean  "publish"
t.boolean  "approved"
end

add_index "stores", ["customer_id"], name: "index_stores_on_customer_id"

和store.rb文件

class Store < ActiveRecord::Base
belongs_to :customer
has_many :products
mount_uploader :picture, PictureUploader
validates :customer_id, presence: true
validates :store_name, presence: true, length: {maximum:20}
#validates :store_description, presence: true, length: {minimum:100 , maximum:1000}, :on => :update 
end

1 个答案:

答案 0 :(得分:1)

Rails日志应该显示ActiveRecord生成的SQL。这似乎没有发生 当您尝试直接在Rails控制台中更新此模型时会发生什么?

$ rails c
$ store = Store.first()
$ store.update_attribute :publish, true

您应该看到类似的内容:

SQL (0.3ms)  UPDATE "store" SET "publish" = ?, "updated_at" = ? WHERE "store"."id" = ?  
[["publish", "t"], ["updated_at", "2016-06-10 00:21:34.754662"], ["id", 1]]
(5.5ms)  commit transaction

您还可以简化此操作并添加一些可能有帮助的内联调试,如下所示:

    def publish
        @store = Store.find(params[:store_id])
        publish_value = params[:publish_value]=="1"

        if @store.update_attribute :publish, publish_value
            publish_value ? @message_type="success" : @message_type="warning"
            publish_value ? @message="Your store has been published successfully." : @message="Your store has been unpublished."
            puts "*** update_attribute succeeded.  @store is:"
            puts @store.inspect
            respond_to do |format|
                format.js
            end
        else 
            @message_type = "error"
            @message = @store.errors.full_messages.join(', ')
            puts "*** update_attribute failed.  @store is:"
            puts @store.inspect
            respond_to do |format|
                format.js { render :template => "layouts/messages.js.erb", :layout => false }
        end
    end