CarlosG90 CarlosG90 -3 years ago 67
Ruby Question

Allow a default false boolean from database to change once button is clicked

Currently working on an app in Rails that has users accept/reject requests from other users. I have a boolean in my database,

t.boolean :accepted, :default => false
that has a default value false and I'm trying to have the value change to true if a user clicks the "accept" button.

I know I'm going to have to make a form that will do a POST/PATCH request but i'm unsure on how to change that value.

Answer Source

I know I'm going to have to make a form that will do a POST/PATCH request but i'm unsure on how to change that value.

You don't need a form here. As you are just trying to update a model instance with a button click, you just need a link_to with a route that is defined to perform this action. Considering your model is Request,the below should work

<%= link_to "Accept", accept_request_path(request) %>

Where request is the instance which need to be updated.

#routes.rb
patch /requests/:id/accept, to: requests#accept, as: :accept_request

or if you want a resourceful route, you can do

resources :requests do
  patch 'accept', on: :member
end

And in the requests_controller have a accept method with below code

def accept
  @request = Request.find(params[:id])
  @request.update_attribute(accepted: true)
end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download