I am attempting to use the button_to helper to remotely update my database, though i am having a few issues, where it doesnt seem to be passing the params.
within my view i am using
- @availabilities.each do |a|
=button_to 'Accept', { :controller => 'availabilities', :action => :update, :id => a.id, :available => true }, :confirm => 'Are you sure?', :method => :post, :remote => true
and in the controller
# PUT /availabilities/1
# PUT /availabilities/1.json
def update
@availability = Availability.find(params[:id])
respond_to do |format|
if @availability.update_attributes(params[:availability])
format.html { redirect_to @availability, :notice => 'Availability was successfully updated.' }
format.js
else
format.html { render :action => "edit" }
format.js
end
end
end
console output
Started POST "/availabilities/2/edit?available=true" for 127.0.0.1 at 2013-02-25 21:43:30 +1100
ActionController::RoutingError (No route matches [POST] "/availabilities/2/edit"):
It looks like you've got the default routes set up which would match a POST
to the create action of the controller and reject any POST
ed data for an update to an existing object.
In the code for your button, change the method to PUT
.
=button_to 'Accept', { :controller => 'availabilities', :action => :update, :id => a.id, :available => true }, :confirm => 'Are you sure?', :method => :put, :remote => true
In a helper I have the following which is displayed in the view.
def toggle_admin(user)
if user.is_admin?
button_to "Yes", toggle_admin_path(user), :id => "toggle_admin_#{user.id}", :class => "btn btn-mini toggle-admin", :remote => true
else
button_to "No", toggle_admin_path(user), :id => "toggle_admin_#{user.id}", :class => "btn btn-inverse btn-mini toggle-admin", :remote => true
end
end
My routes file points the toggle_admin_path
to a user settings controller which contains the following:
def toggle_admin
@user = User.find(params[:id])
@account = @user.account
if @user.is_admin? && @account.admins > 1
@user.remove_role :admin
else
@user.roles << :admin
end
if request.xhr?
render :status => 200, :content_type => 'text/javascript'
else
redirect_to edit_account_path
end
end