1

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"):
Boss Nass
  • 3,384
  • 9
  • 48
  • 90
  • 1
    the problem seems to be with the route, make sure the url to which you are posting data exists that is the controller, action method and matching of the input params – Dakait Feb 25 '13 at 10:56

1 Answers1

1

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 POSTed 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

Example code following comment discussion

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
Robin Fisher
  • 1,454
  • 1
  • 13
  • 23
  • put method seems to be the goods, but its still not passing the params :S – Boss Nass Feb 25 '13 at 11:13
  • im getting `(0.2ms) BEGIN Availability Exists (0.6ms) SELECT 1 FROM "availabilities" WHERE ("availabilities"."user_id" = 6 AND "availabilities"."id" != 2 AND "availabilities"."event_id" = 2) LIMIT 1 (0.3ms) COMMIT` – Boss Nass Feb 25 '13 at 11:13
  • When you have your availability object, what are you trying to do with it? Set the `available` attribute to true? Your controller expects there to be a hash called availability that is passed (`update_attributes(params[:availability]`) but this doesn't exist. – Robin Fisher Feb 25 '13 at 11:20
  • ended up doing `=button_to 'Accept', { :controller => 'availabilities', :action => :update, :availability => { :available => true }}, :confirm => 'Are you sure?', :method => :put` though i dont think this is ideal – Boss Nass Feb 25 '13 at 11:27
  • I have a similar scheme in one of my apps which toggles admin status for a user. I have a separate route defined called `toggle_admin` which routes to a `settings` controller. A method in the controller does the one job of toggling the person's admin status. The content of the button then varies depending on if the person is an admin. I could post some sample code in my answer if you want to go that route? – Robin Fisher Feb 25 '13 at 11:28
  • just trying to achieve the best coding standards, my method seems a bit hack, that would be fantastic then i can gauge where i have gone different – Boss Nass Feb 25 '13 at 11:30
  • Great stuff. No reason you can't use that solution and removes duplication. – Robin Fisher Feb 25 '13 at 11:30
  • using your method would be less likely of possible sql injection? – Boss Nass Feb 25 '13 at 11:41