wellcole
wellcole

Reputation: 21

Button click changes boolean to true

I am trying to make a button that is clicked that changes a boolean(clue1) in a model(clue) to true. I have read that I have to use a form rather than Link_to. I would rather use a link to because I want it to redirect it to another page after it's submitted. From posts online I came up with this:

<%= form_for :clue1 do |f| %>
<div><%= f.hidden_field :clue1, :value => true %></div>
<%= f.submit "See Next Clue", class: "btn btn-default"%>
<% end %>

But after many attempts I would just get errors. Thanks for your help.

Upvotes: 1

Views: 2040

Answers (2)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

If you have a form/link like this, you can do this by adding another route and passing no params. Like this...

Add a route to update this boolean

resources :clue do
  patch :update_clue1, on: :member
end

Add link_to with method

= link_to "Update Clue 1", update_clue1_clue_path(clue), method: :patch

and in your controller, add an action

def update_clue1
  @clue = Clue.find(params[:id]
  @clue.update_attribute(:clue1, true)
  redirect_to :other_page
end

Upvotes: 2

Kalyani
Kalyani

Reputation: 31

If you have to set a variable with boolean value you can use "remote: true" with link_to tag and create a .js file for that submit action and in that js file you can set the variable value you want.

Upvotes: 0

Related Questions