title: Update a record with a button in Rails?
body: <p>I'm trying to update a record in my Rails app using a button. I have a User and I want to update its school_id value. I have a School view page where a User can click on a button to add that school's id to the User school_id field. I'm struggling with the implementation. Here's what I have so far:</p>
  
  <p>User controller:</p>
  
  <pre><code>def add_school
      @user = User.find(params[:user_id])
      @user.update_attributes(:school_id)
  
      respond_to do |format|
      flash[:notice] = "School has been added!"
      format.html { redirect_to :back }
      format.js
  end
  </code></pre>
  
  <p>Button on School show page:</p>
  
  <pre><code>&lt;%= button_to "Add School", add_school_user_path, :method =&gt; "put" %&gt;
  </code></pre>
  
  <p>I tried to do this a different way by just adding code to the update action in the User controller but I couldn't get that to work either:</p>
  
  <pre><code>def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:school_id])
      flash[:notice] = "School has been added!"
      redirect_to @user
    end
    if @user.update_attributes(params[:user])
      flash[:notice] = "Successfully updated account and profile!"
    end
  end
  </code></pre>
  
  <p>What's the best way to pass the School's id into the User school_id column?</p>
  
  <p>Thanks!!</p>
  
  <p>EDIT 1: Routes</p>
  
  <pre><code>resources :users do
      member do
        get :following, :followers
        put :add_school
      end
  </code></pre>
  
  <p>Updated controller:</p>
  
  <pre><code> def add_school
      @user = current_user
      @school = School.find(params[:id])
      @user.update_attributes(school_id: params[:school_id])
  
      respond_to do |format|
      flash[:notice] = "School has been added!"
      redirect_to @user.school
      end
    end
  </code></pre>
  
  <p>Updated button link:</p>
  
  <pre><code>&lt;%= button_to "Add School", add_school_user_path(@user, school_id: @school.id), :method =&gt; :put %&gt;
  </code></pre>
  
  <p>Routing error:</p>
  
  <pre><code>No route matches {:action=&gt;"add_school", :controller=&gt;"users", :school_id=&gt;1, :id=&gt;nil}
  </code></pre>