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
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.
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.if to show one of two possible states. What you're asking for is exceedingly specific. Hopefully you can adapt that example as required.