3

I am still fairly new to RoR and I am trying to delete an object with a button_to delete button. With the code I wrote though, it gets me to /needs.4 instead of /needs/4 when I try to get it to /needs/:id for the destroy method. A "need" is created via the needs-controller and a needs new.html.erb page, and then shows up in the user's show page. from there, a user is supposed to be able to delete his/her need. This is the error I get:

ActiveRecord::RecordNotFound in NeedsController#destroy

Couldn't find Need with id=@userneed
Rails.root: /Users/mcn/Dropbox/Code/GA/Projects/GA_projects/p4_final/flatcircle

Application Trace | Framework Trace | Full Trace
app/controllers/needs_controller.rb:20:in `destroy'
Request

Parameters:

{"_method"=>"delete",
 "authenticity_token"=>"Fv6EcMNJQEjtw1naQVMw77lkCGjTJR7ui2FD53aoZfc=",
 "id"=>"@userneed"}

And this is my code:

Needs_controller:

def destroy
    Need.find(params[:id]).destroy
    redirect_to :controller => :users, :action => :show, :id => current_user.id, :flash => { :success => "Your search post was deleted." }
  end

User's show page button_to line:

  <%= button_to "delete", '/needs/@userneed', method: :delete, data: { confirm: "You sure?"} %>

and on same page:

@userneed = @current_user.needs.last

Routes.rb

delete "/needs/:id", to: "needs#destroy"
get "/needs/:id", to: "needs#show"

Super confused, let me know if you know how to solve it!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mcn
  • 45
  • 5

3 Answers3

1

Ok so this is how I fixed it:

<%= button_to "delete", {:controller => :needs, :action => "destroy", :id => current_user.needs.last.id}, :method => :delete, data: { confirm: "You sure?"} %>

So I guess it was two things: 1) the curly braces in the right places (I need them because there are still arguments to come after the stuff in the curly braces 2) specifying the id this way instead of with the _path() way

mcn
  • 45
  • 5
0

try <%= button_to "delete", '/needs/<%= @userneed %>', method: :delete, data: { confirm: "You sure?"} %>

and @userneed = @current_user.needs.last.id

But I think its best to use a link instead a button...something like <a href="<%=model_path(@model) %>" data-method="delete" data-confirm="are you sure?">delete</a>

Luiz E.
  • 6,769
  • 10
  • 58
  • 98
  • Thanks,will try. Had link_to before keep you posted. I am wondering: I writing the button_to in the user's show page.so do I have to call on the Needs_controller that has the destroy method in it? – mcn Dec 24 '13 at 13:12
  • Luiz, with your edits, I get this error: SyntaxError in Users#show Showing /Users/mcn/Dropbox/Code/GA/Projects/GA_projects/p4_final/flatcircle/app/views/users/show.html.erb where line #126 raised: /Users/mcn/Dropbox/Code/GA/Projects/GA_projects/p4_final/flatcircle/app/views/users/show.html.erb:126: syntax error, unexpected $undefined, expecting ')' ...);@output_buffer.safe_concat('\', method: :delete, data: { c... ^ Extracted source (around line #126): 126: <%= button_to "delete", '/needs/<%= @userneed %>', method: :delete, data: { confirm: "You sure?"} %> – mcn Dec 24 '13 at 13:16
0

You need to run rake routes and then see which path you need. Example...

          schemas GET    /schemas(.:format)                              schemas#index
                  POST   /schemas(.:format)                              schemas#create
       new_schema GET    /schemas/new(.:format)                          schemas#new
      edit_schema GET    /schemas/:id/edit(.:format)                     schemas#edit
           schema GET    /schemas/:id(.:format)                          schemas#show
                  PUT    /schemas/:id(.:format)                          schemas#update
                  DELETE /schemas/:id(.:format)                          schemas#destroy

Take the route name on the left, so for me to get to the SchemasController and it's destroy action I need to use one of the route path helpers...

schemas_path(@schema)

That would automatically be replaced with at runtime (if the object's ID was 1)...

/schemas/1

So to use that in a button...

<%= button_to "delete", schemas_path(@schema), method: :delete, data: { confirm: "You sure?"} %>

You should always use the route helpers when referencing routes as it allows you to change all of them by adjusting the routes.rb file. If you need to read more on Rails Routing, the guide can be found here...

http://guides.rubyonrails.org/routing.html

lloydpick
  • 1,638
  • 1
  • 18
  • 24
  • thanks, yes i did that. tried about a ton of versions of writing this before ^^ so with your suggestion I get Routing Error No route matches [DELETE] "/needs". It is not taking in the id of the object but I don't know why it doesn't do it. – mcn Dec 24 '13 at 13:20