简体   繁体   中英

Update a record with a button in Rails?

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:

User controller:

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

Button on School show page:

<%= button_to "Add School", add_school_user_path, :method => "put" %>

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:

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

What's the best way to pass the School's id into the User school_id column?

Thanks!!

EDIT 1: Routes

resources :users do
    member do
      get :following, :followers
      put :add_school
    end

Updated controller:

 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

Updated button link:

<%= button_to "Add School", add_school_user_path(@user, school_id: @school.id), :method => :put %>

Routing error:

No route matches {:action=>"add_school", :controller=>"users", :school_id=>1, :id=>nil}

You need a form for that instead of just abutton

<%= form_tag add_school_user_path(@user), method: put do -%>
  <%= hidden_field_tag :school_id, @school.id -%>
  <%= submit_tag 'Add school' -%>
<%- end -%>

you didn't provide the context code, maybe @user and @school are not the real variable names but you can get the idea from this

You need to pass the whole hash into the call to .update_attributes .

Preferably, you will put school_id inside of user , so it will look like

# params[:user] = { school_id: 1 }
@user.update_attributes(params[:user])

Or you could code the school id manually

@user.update_attributes(school_id: params[:school_id])

Or, better yet, validate the association

@user.school = School.find(params[:school_id]

The path you would want is

user_add_school_path(@user, school_id: 1)

This is an old Q, but nevertheless for the sake of completeness. You do not necessarily need a form for a simple form-alike submission with a button/link. You can rely on jquery-ujs and data-params .

<%= link_to add_school_user_path(@user), class: "btn btn-xs btn-primary",
  title: "Add school",
  data: {remote: true, confirm: 'Are you sure?', method: 'put',
    params: {user: {schoold_id: @school.id}}} do %>
      <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
      Add school&hellip;
<% end %>

Note that you'd want to have school_params method in your controller akin to params.require(:user).permit(:school_id)

The provided answer is no longer entirely accurate. You can pass data and update a record with a button_to, so long as you aren't attempting to pass arbitrary data.

The button_to syntax would be something like this -- please note, this is my implementation of it, and it is not adjusted to your specific implementation as your controller would need additional adjustments from what you've specificed to accept certain params --:

<%= button_to "Mark Completed", todo_path(todo.id),   
    params: {:todo => { :id => todo.id, :completed => true, :user_id => current_user.id }}, 
    method: :put, data: { confirm: "Mark this To-Do as being completed?  This will hide it from view." } %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM