title: Rails 3 how do I use link_to to change the value of a boolean in the db?
body: <p>I'm trying to create a simple link that will allow an admin to approve a user on my website </p>
  
  <p>quite similar to what this guy was trying : <a href="https://stackoverflow.com/questions/7124781/in-rails-3-how-do-i-use-button-to-to-change-the-value-of-a-boolean">In Rails 3 how do I use button_to to change the value of a boolean?</a></p>
  
  <p>Here's how it was supposed to work :</p>
  
  <ol>
  <li>Admin clicks the link to approve a user</li>
  <li>The link calls the activate function in UsersController</li>
  <li>The active function calls that users model </li>
  <li>the user model updates the approved attribute to true and saves it</li>
  </ol>
  
  <p>and here's how I was going to do it </p>
  
  <p>in my users#index view</p>
  
  <pre><code> &lt;% @users.each do |user| %&gt; 
   &lt;%= link_to 'Approve', :action =&gt; "index", :method =&gt; 'activate', :id =&gt; user.id, :class =&gt; 'btn btn-mini btn-danger' %&gt; 
  &lt;% end %&gt; 
  </code></pre>
  
  <p>in my userController</p>
  
  <pre><code>def activate
    @user = User.find(params[:user])
    @user.activate_user
  end
  </code></pre>
  
  <p>in my user model</p>
  
  <pre><code>def activate_user 
    self.approved = 'true'
    self.save
  end
  </code></pre>
  
  <p>and my routes </p>
  
  <pre><code>devise_for :users, :controllers =&gt; { :registrations =&gt; "registrations" }
  
  resources :users do
    member do
      get 'activate'
      put 'activate'
    end
  end
  
  match "users/:id/activate" =&gt; "users#activate"
  </code></pre>
  
  <p>Now when I click the link (to approve users), I'm sent back to the user index page (like I'm supposed to) but the user field "approved" is still set to false :I</p>
  
  <p>The URL I get after clicking the link : </p>
  
  <pre><code>  http://localhost:3000/users?class=btn+btn-mini+btn-danger&amp;id=2&amp;method=activate
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As suggested iv added the URL to the link_to helper</p>
  
  <pre><code>&lt;% @users.each do |user| %&gt; 
    &lt;%= link_to "Approve", :controller =&gt; "users", :id =&gt; user.id, :action =&gt; "activate", :method =&gt; :put, :approved =&gt; true %&gt; 
  &lt;% end %&gt;
  </code></pre>
  
  <p>When I click the helper link I get </p>
  
  <pre><code>wrong number of arguments (1 for 2) 
  </code></pre>
  
  <p>in app/controllers/users_controller.rb:7:in `activate'</p>
  
  <pre><code>def activate
    @user = User.find(params[:id])
    @user.update_attribute(params[:user])
    redirect_to "/users?approved=false"
  end
  </code></pre>
  
  <p>line 7 where the error is 
      @user.update_attribute(params[:user])</p>
  
  <p>What else should I put there? </p>
  
  <p>oh and here is my route for this method</p>
  
  <pre><code>match "/users/:id/activate" =&gt; "users#activate"
  </code></pre>
  
  <p><strong>UPDATE V2</strong>
  So I have changed that update line to :</p>
  
  <pre><code>@user.update_attributes(@user.approved, "true")
  </code></pre>
  
  <p>and it seems to do everything i want it to do except changing the value to true ! </p>
  
  <p>I've also tried to use 1 for true (and the update_attribute function) and non-strings.. running out of ideas here lol</p>
  
  <p><strong>The solution</strong></p>
  
  <p>Well this is awkward but so it happens that in my user model i had <code>attr_accessor :approved</code>  this resulted in that the model never went to the database to update the <code>:approved</code> column BUT instead it updated the local variable <code>:approved</code> so next time when I looked at the column then of course the <code>:approved</code> value had not changed</p>
  
  <p>tldr? 
  if you have attr_accessor in your model with the same name as the column your trying to update => remove it</p>