0

I have boolean field in the database, how can I update it in rails using link_to. I want to have two link_to commands. One for true another for false.

how do i do it? I can use ajax, but i wanna learn to pass the data first.

Thanks

1 Answer 1

5

link_to can only link to a target controller action. You need to define a route that will route the call to a method that toggles the boolean value.

For example, in your controller:

class ThingsController
  def toggle_foo
    @thing = Thing.find(params[:id])
    @thing.foo = [email protected]
    @thing.save
  end
end

Then you would route this:

resources :things,
  :member => { :toggle_too => :put }

Then you can link to it:

link_to('Toggle', toggle_foo_thing_path(@thing), :method => :put)

It's important to not use a GET method on these calls because some browsers will pre-load all the simple links on your page, which will have the effect of automatically toggling all those things you link to on the page.

Sign up to request clarification or add additional context in comments.

2 Comments

I have a field in db which says products which is boolean. in the code above how do I toggle? Plus i am looking to have two buttons Agree and disagree. If agree is hit then its true, else otherwise.
Well, that's a case of setting the appropriate label, maybe having an if to show one of two possible states. What you're asking for is exceedingly specific. Hopefully you can adapt that example as required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.