Ruby on Rails- Destroy record using link - ruby-on-rails

I want to delete a record that is stored in a table using a link right next to the table data. The error I come up with is:
No route matches [GET] "/carlogs/destroy"
My destroy method:
def destroy
#carlog= CarLog.find(params[:id])
#carlog.destroy()
redirect_to show_path(#carlog)
end
Part of the view code containing the Delete link:
<% #carlogs.each do |car| %>
<tr>
<td><%= car.id %></td>
<td><%= car.plate_number %></td>
<td><%= car.brand %></td>
<td><%= car.slot_number %></td>
<td><%= car.is_taken %></td>
<td><%= car.created_at %></td>
<td><%= link_to "Delete", show_path(car), method: :delete, data:
{confirm: "Are you sure?"} %>
</tr>
<% end %>

Make sure that you have delete REST method as:
DELETE /carlogs/:id(.:format) carlogs#destroy
And in your application.js you must have this line:
//= require jquery_ujs

Use destroy link for destroy record then redirect table like
method
def destroy
#carlog= CarLog.find(params[:id])
#carlog.destroy()
redirect_to show_path(#carlog) #-> Table pathe
end
routes.rb
delete 'destroy' => 'carlogs#destroy'
View
<%= link_to "Delete", destroy_path, method: :delete, data:
{confirm: "Are you sure?"} %>
I think will help you

Why did you write show_path(car)?
Maybe you mean car_path(car) ?
<%= link_to "Delete", car_path(car), method: :delete, data:
{confirm: "Are you sure?"}%>
Also you should check your route [GET] "/carlogs/destroy.
I think this don't present in rake router

Related

Delete keeps going to the Show page on rails application

I'm trying to create a blog in rails, and I'm having a little trouble. I keep getting the page for my show method each time I try to use my destroy method to test deleting an article from my blog. This is the destroy method:
def destroy
#article = Article.find(params[:id])
#article.destroy
flash[:notice] = "Article has been deleted."
redirect_to articles_path
end
And this is the index file for the home page:
<h1>Listing all articles</h1>
<p>
<%= link_to "Create new artilce", new_article_path %>
</p>
<table>
<tr>
<th>Titile</th>
<th>Text</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
</tr>
<% end %>
</table>
If someone could please tell me where the faulty code is, I would greatly appreciate it.
Almost certainly a javascript / jquery issue.
Rails sets the "method" with a form, using JQuery to define it explicitly:
This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET
Thus, if you have Javascript / JQuery disabled, your link_to will simply be GET, which sends the request to the show action.
--
You need to make sure you have the following:
#app/views/layouts/application.html.erb
<%= javascript_include_tag :application %>
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs

Using link_to to trigger an increment action in a controller

I have been butting my head against a wall trying to figure this sucker out.
Basically, I have a 'Quote' model that has 3 fields - content, author and votecount.
Votecount is an integer, and I want to be able to add a vote (increment) from the quotes/index view using a link. This is what I've come up with so far:
views/quotes/index.html.erb
<% #quotes.each do |quote| %>
<tr>
<td><%= quote.content %></td>
<td><%= quote.author %></td>
<td><%= quote.votecount %></td>
<td><%= link_to 'Show', quote %></td>
<td><%= link_to 'Edit', edit_quote_path(quote) %></td>
<td><%= link_to 'Upvote', quote_upvote_path(quote) %></td>
<td><%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
quotes_controller.rb
def upvote
#quote = Quote.find(params[:id])
#quote.increment!(:votecount)
redirect_to quotes_path
end
Routes.rb
resources :quotes do
get 'upvote'
end
And this is the error message I receive:
ActiveRecord::RecordNotFound in QuotesController#upvote
Couldn't find Quote with 'id'=
So the action isn't able to find the quote ID, however it's in the actual URL so I'm not sure what I'm bollocksing up here!
Define the upvote action as a member action:
resources :quotes do
get 'upvote', on: :member
end
See the Rails routing documentation for more information.

URL link gives dot instead of slash

in my route.rb I have this
Rails.application.routes.draw do
resources :cars do
resource :payments
end
end
However, in my destroy link for payments. the URL generated is
http://localhost:3000/cars/9/payments.11
Below is my code.
<% #car.payments.each do |p| %>
<tr>
<td><%= p.date %></td>
<td><%= p.profit %></td>
<td><%= p.remark %></td>
<td><%= link_to 'Delete', car_payments_path(#car, #p) ,
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Please advice. Thank you in advanced.
Looks like this is a pluralization error.
Try
cars_payment_path
instead of
car_payments_path
To delete a payment in a car, the route should be a member route , call it like this:
car_payment_path(#car, #p)
car_payments_path(..) was a collection route of payments.
Suggest you to test at console like this:
app.car_payment_path(Car.first, Car.first.payments.first)

Unable to find "Destroy" link

I am trying to write test scenario for delete,but i don't understand why it is not getting destroy link.
Here my test scenario:
Scenario: User can delete kids
Given I am on the kids page
When I Destroy kid
Then I should see "Kid deleted successfully"
Then one kid should not exist
<h1>Listing kids</h1>
<tr>
<th>Kid name</th>
<th colspan=3>Action</th>
</tr>
<% #kids.each do |kid| %>
<tr>
<td><%= kid.kid_name %></td>
<td><%= link_to 'Show', kid %></td>
<td><%= link_to 'Edit', edit_kid_path(kid) %></td>
<td><%= link_to 'Destroy', kid, method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'New Kid', new_kid_path %>
My step defination for link:
When /^I Destroy kid$/ do |link|
click_link(link)
end
Please somebody suggest step definition for delete link, please correct me if their is some error in my scenario.
Thank you.
I have found the answer for destroy link. It won't support webrat so i have removed webrat and used capybara and my issue get solved.
Here is the step:
Scenario: Delete Kid
Given I am on the kids page
And there is a kid with kid_name "john"
When I destroy that kid
Then I am on the kids page
step_defination:
When /^I destroy that (.*)$/ do |element_type|
element = element_type.classify.constantize.last
path = "#{element_type}_path"
case page.driver
when Capybara::RackTest::Driver
page.driver.submit :delete, send(path, element), {}
else
visit send(path, element, { method: :delete })
end
end </code>

If Statement within Views in Rails 3

Ok so i have been starting to get used to rails 3 over the past few days and have got a project in the works to test things out on. Is it possible to do the following or what would you suggest is the best way to only allow post authors to edit their posts.
<% if post.author_id == current_user.id %>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
Recommendation: Don't compare ids - compare objects.
<% if post.author == current_user %>
Optional: Consider using a plugin (only if necessary) like cancan to make it even more descriptive.
<% if can? :edit, post %>

Resources