views:

44

answers:

1

This seems incredibly similar to a question I had answered just a few days ago, but the solution then isn't working now.

I'm building a rails app, and I am trying to have a button_to trigger a destroy in a different controller.

the code I have for the button is

<%= button_to "delete", :controller => :meals, 
                           :action => 'destroy',
                           :recipe_id => recipe.id,
                           :method => :post >

when I click the delete button, i get a 'no matches for meals/3' which is the current meal_id.

the destroy in the meals controller looks like this

  def destroy
    @meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first
    @meal.destroy

    respond_to do |format|
      format.html { redirect_to :controller => "user" , :action => "show" }
      format.xml  { head :ok }
    end
  end

it appears as though the button_to is completely ignoring the :action and requesting show which does not exist and shouldn't exist.

+1  A: 

And how you part of routes.rb for that one looks like? Because if you use map.resources then destroy has same path as show but :method => :delete(which is virtual verb implemented by form and _method=delete param).

Try this:

<%= button_to "delete", {:controller => :meals,
      :action => 'destroy', :id => recipe.id }, :method => :delete %>

or if recipe is instance of Meal class then

<%= button_to "delete", @recipie, :method => :delete %>

Mind the curly brackets.

gertas
I'm not entirely sure what you are saying. my route is "resources :meals". I changed :method => :post to :method => :delete but for some reason that is now pointing to create
pedalpete
if yoyu have resources :meals then `:id` is expected by route, not `:recipe_id`. Just run `rake routes` to see what routes are registered.
gertas
I removed the recipe_id from the button_to but I still get the same No Route Matches "/meals/3" error. I ran rake routes, and my meals route has "meal DELETE /meals/:id(.:format) {:action => "destroy", :controller =>"meals"}. My button_to form action is "meals/3?method=delete", which seems right to me.
pedalpete
I've updated answer.
gertas
thanks gertas. that is the correct answer. the REASON it is correct is the most important. A friend filled me in that if a hash is the last argument, then curly braces are not required. However if their is an argument after the hash, curly braces are required. This gets very confusing when tutorials, etc. will show no curly braces because the hash is the last argument, but when a beginner like me knows enough to put :method => delete, but doesn't know about the sometimes yes, sometimes no curly braces rule.
pedalpete
In rails api docs, which are authoritative source of information, the example has proper bracketing. Also checking method source helps understand usage. In general Rails 3 source code is easy to read and gives full of tips how to use Ruby effectively.
gertas