Reputation: 459
I've scraped products from a website and inserted them into my DB. All the products are being listed properly on my View page, but I can't seem to get the delete button to work. The reason there are repeats in my rake routes is that I initially wrote the routes out manually but then used
resources :ibotta
I just tried moving the "resources :ibotta" to the top of the routes, but that didn't work. When I click the "Destroy" button, the link it takes me to is
"https://rails-tutorial2-chriscma.c9users.io/ibotta.14738"
Any help is much appreciated, thanks.
View
<h1>Show Page for iBotta</h1>
<h3><%= @products.length %> products in the iBotta DB</h3>
<% @products.each do |x| %>
<p>Title: <a href=<%=x.link%>><%= x.title %></a> </p>
<p>Value: <%= x.values %> </p>
<p>Store: <%= x.store %> </p>
<%= link_to 'Destroy', ibotta_path(x.id),
method: :delete %>
<% end %>
Method in Controller
def destroy
Ibotta.find(params[:id]).destroy
redirect_to ibotta_path
end
Rake Routes
ibotta_save GET /ibotta/save(.:format) ibotta#save
ibotta_show GET /ibotta/show(.:format) ibotta#show
ibotta_delete GET /ibotta/delete(.:format) ibotta#delete
ibotta GET /ibotta(.:format) ibotta#index
POST /ibotta(.:format) ibotta#create
new_ibottum GET /ibotta/new(.:format) ibotta#new
edit_ibottum GET /ibotta/:id/edit(.:format) ibotta#edit
ibottum GET /ibotta/:id(.:format) ibotta#show
PATCH /ibotta/:id(.:format) ibotta#update
PUT /ibotta/:id(.:format) ibotta#update
DELETE /ibotta/:id(.:format) ibotta#destroy
Upvotes: 1
Views: 1834
Reputation: 33420
Try just passing your object and using the delete method to make a DELETE request, like:
<% @products.each do |product| %>
<p>Title: <%= link_to product.title, product.link %></p>
<p>Value: <%= product.values %></p>
<p>Store: <%= product.store %></p>
<%= link_to 'Destroy', product, method: :delete %>
<% end %>
In case of creating an a
tag, you can use the link_to
Rails helper.
As I saw in your project, you don't have a layouts/application.html.erb
file, that's why everything you render isn't passing by the yield
in this file, and you're not adding nothing in your application.js or css file, so, you don't have jQuery nor jQuery UJS. This makes everytime you click on the anchor tag to delete such element, to perform a GET request, no matter if you're specifying the method to use.
This can be solved by adding the folder and file, as with any project, with the initial structure:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Note if you do it, you need to comment the content in your javascripts/ibotta.coffee
, otherwise you'll get the error:
SyntaxError: [stdin]:6:8: unexpected @
And I'm not sure why.
Or if you prefer to continue without this file (which I don't recommend), you can easily just to change your link_to
helper, for a button_to
helper, like:
<%= button_to 'Destroy', x, method: :delete %>
What'll give tou a different html structure, but works on deleting the records:
<form class="button_to" method="post" action="/ibotta/id">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="Destroy">
<input type="hidden" name="authenticity_token" value="token">
</form>
Note you have a destroy and delete method in your controller, I guess the one you need is destroy
, which is used by your resources
routes.
Here is the repository where you can see it working.
Upvotes: 1