Reputation: 619
I have a model store with a controller. This controller has an action "publish" which sets the store :publish field true or 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
When I publish the store params[:store_id] and params[:publish_value] are successfully passed to the controller. And I receive "Success" message. But for some reason when I check the rails console or refresh the page there is no change in :publish value. It still shows as nil.
Here is the log after clicking publish button.
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)
Help will be appreciated. Thanks.
Also here is the schema
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"
and the store.rb file
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
Upvotes: 0
Views: 197
Reputation: 459
The Rails log should show your SQL generated by ActiveRecord. That doesn't seem to be happening.
What happens when you try to update this model directly in Rails console?
$ rails c
$ store = Store.first()
$ store.update_attribute :publish, true
You should see something like:
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
You can also simplify this action and add some inline debugging that might help, like so:
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
Upvotes: 1