mcn
mcn

Reputation: 45

RoR how to get button_to path to /needs/4 instead of /needs.4?

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!

Upvotes: 3

Views: 202

Answers (3)

mcn
mcn

Reputation: 45

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

Upvotes: 1

lloydpick
lloydpick

Reputation: 1649

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

Upvotes: 0

Luiz E.
Luiz E.

Reputation: 7279

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>

Upvotes: 0

Related Questions