Update a boolean field in db using link_to

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

Answers


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 = !@thing.foo
    @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.


Need Your Help

requirejs anonymous dependency not defined

javascript requirejs dependencies

I have a problem with requirejs and a dependency being undefined.

iOS Web App doesn't set cookie in the first place

ios ajax cordova cookies ruby-on-rails-4.2

I have seen lots of questions and answers on how to permanently store a cookie in an iOS web application... But I can't seem to even get this one set.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.