AnApprentice
AnApprentice

Reputation: 111080

Rails - Posting a Boolean (true or False) and updating the DB in the controller

in my Rails 3 App, I'm posting with jQuery the following:

http://0.0.0.0:3000/topics/read_updater?&topic_id=101&read=true

In my controller:

  def read_updater
      current_user.topics.where(:topic_id => params[:topic_id]).update_all(:read => params[:read])

    render :json => {:status => 'success' }
  end

params[:topic_id] works great, but params[:read] is inserting empty values in the DB, even though jQuery is posting either a true or false. Ideas? I want rails to update the DB with either true or false.

Thanks

Upvotes: 1

Views: 1259

Answers (1)

klew
klew

Reputation: 14977

Maybe update_all isn't parsing passed string? params[:read] is a string:

params[:read]
=> "true"
# or
=> "false"

So maybe it is trying to save a string to this column and that's why you have problems.

Try this line:

current_user.topics.where(:topic_id => params[:topic_id]).update_all(:read => true)

And see if it is working. If yes, then replace :read => params[:read] with something like :read => params[:read] == 'true'.

I also hope that you've checked how params hash looks in this controller. If not, just put:

logger.debug params.inspect

at the begining of read_updater method and check logs if everything is fine.

Upvotes: 2

Related Questions