Unable to find "Destroy" link - ruby-on-rails

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>

Related

Rails turn URL string into hyperlink

So I'm iterating over a table to kick out link title and link urls for each additional item thats being added by a user. Ultimately I want a very basic:
Title, URL
I need the URL to be clickable and I'm hitting a wall.
<tbody>
<% #links.each do |link| %>
<tr>
<td><%= link_to link.url %></td>
<td><%= link.title %></td>
<td><%= link_to 'Show', link %></td>
<td><%= link_to 'Edit', edit_link_path(link) %></td>
<td><%= link_to 'Destroy', link, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Right now the link_to link.url will display the correct link but the link redirects back to the homepage. I've also tried:
url_to link.url
error message that says "did you mean url_for"
url_for link.url
which removes a hyperlink
link_to("#{link.url}")
which has the same issue my code above in that it links back home
link_to("#{#link.url}")
which returns an error of: undefined method `url' for nil:NilClass
link_url("#{link.url}")
returns the entire localhost address and then the link...not a hyperlink
auto_link(link.url)
returns an error asking if I meant autoload
Surely I'm missing something that's super easy.
for your reference link_to
the format is
link_to(body, url, html_options = {})
# url is a String; you can use URL helpers like
for your problem
<td><%= link_to link.url, "http://#{link.url}" %></td>

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)

Returning data from database to another page in rails

How can I return data from the database on another page?
I can file this under: views / posts / index.htm.erb
<h1>Listing posts</h1>
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Post', new_post_path %>
And I wanted to display these values ​​on another page: views / home / blog.html.erb
How do I do this? Could you explain all the steps so that I can display these values ​​in my other page.
Thanks Kocur4d
but how do I get only some information? eg: I would like that to appear in the title of the post page like this way (in blog.html.erb):
<div class="post-title">
<h2 class="title"> <a href="link_to_post"> **<% = post.titulo%>** </ a> </ h2>
</ div>
Step 1. Create controller
In your app root directory run:
rails g controller home blog
Modify controllers/homes_controller.rb :
class HomesController < ApplicationController
def blog
#posts = Post.all
end
end
Your controllers/posts_controller.rb should be already set up. Minimum what you need for your question is to have index method defined you might have other methods as well:
class PostsController < ApplicationController
def index
#posts = Post.all
end
end
Step 2. Extract Partial
change views/posts/index.htm.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create/modify views/home/blog.html.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create views/shared/_posts.html.erb :
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
Step 3. Set up routes.
You should have something like this in your routes.rb file:
resources :posts or match 'posts/index' => 'posts#index'
add this to config/routes.rb:
match 'home/blog' => 'home#blog'
so it might look like this(there is few variants):
config/routes.rb:
YourAppName::Application.routes.draw do
root to: 'posts#index'
resources :posts
match 'home/blog' => 'home#blog'
end
Now when you start rails server(assuming standard configuration) and visit:
127.0.0.1:3000/posts/index and 127.0.0.1:3000/home/blog
you should see same content.
This should work copy-and-paste but I could make some typos and other small mistakes(hope not, if ill find any ill try to edit them asap). In general look at it as you need 3 steps to forward http request down your rails application stack.
Map url to controllers using routes.
Create controllers and inside prepare data for views.
Display data in Views.
Look around in Rails Guides, Rails for Zombies and Rails Tutorial for more info.
---------Upadate to your second question-----------
I don't really understand what would you like to achieve?? At the moment both index.html.erb and blog.html.erb showing the same data, that was what you ware asking for?
post representing one post and is available in sharde/_posts.html.erb. You can't reference it from index.html.erb or blog.html.erb.
#posts represents all the posts and its available in index.html.erb or blog.html.erb.
render partial: 'shared/posts', object: #posts -- this line say "Hey man! Paste here content of shared/posts file, and btw I have here a local variable #posts so if you need to use that date in shared/posts file Ill name it posts from in side there"
To make them look different modify both files and part that will be identical for both of them is in a sharde/_posts.html.erb.
Try for example remove this line:
<td><%= post.category.name %></td>
from shared file to see what is going to happen.
Add some html tags and thinker with it.
Rails has may helper methods available' to find out about them check the links I give you and google, google, google.
Try to add some links with link_to helper
In your home controller, for the blog method, set #posts as you need...
Maybe
#posts = Post.all

Resources