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>
Related
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.
I know this has to be a duplicate question - I did search for it, but could not find any answers.
I'm trying to do a simple link to some documents from the index view of my rails application. I'm using the wkhmtltopdf plugin via the PDFkit gem. I'm able to simply append the .pdf extension to any page and get a pdf copy. That part works great, I just can seem to figure out the proper syntax to append a format. Here's what I have so far:
<tr>
shortened for brevity's sake
<td><%= link_to 'Show', certification %></td>
<td><%= link_to 'Edit', edit_certification_path(certification) %></td>
<td><%= link_to 'Destroy', certification, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to "Download PDF", certification_path(#certification, :format => "pdf") %></td>
</tr>
I was trying to follow Ryan's method from the PDFkit Railscast Episode, but it seems his method must be deprecated or I might have done something wrong.
<tr>
shortened for brevity's sake
<td><%= link_to 'Show', certification %></td>
<td><%= link_to 'Edit', edit_certification_path(certification) %></td>
<td><%= link_to 'Destroy', certification, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to "Download PDF", certification_path(#certification, :format => "pdf") %></td>
</tr>
Okay from your code it seems you have used certification and #certification. From your comment before which says nil doesn't exist, #certifcation doesn't have any value in it.
So i believe the correct fix in your case is.
<td><%= link_to "Download PDF", certification_path(certification, :format => "pdf") %></td>
Im currently building a blog. While the paperclip photos pop up inside the individual blog posts they don't seem to pop up in the index, where all the blog posts are displayed.
13th line seems to be the problem.
this is my index.html.erb for my posts
10 <% #posts.each do |post| %>
11 <tr>
12 <td><%= link_to post.title, post_path(post) %></td>
13 <td><%= image_tag #post.picture(:medium) %></td>
14 <td><%= post.text %></td>
15 <td><%= link_to 'Show', action: :show, id: post.id %></td>
16 <td><%= link_to 'Edit', action: :edit, id: post.id %></td>
17 <td><%= link_to 'Destroy', { action: :destroy, id: post.id },
18 method: :delete, data: { confirm: 'Are you sure?' } %></td>
it throws me a no method error.
Please let me know if I can give you extra files if you need more information
Thank you in advance!
You're not passing the URL for the image to the image_tag helper.
You need to display your image like this:
<%= image_tag post.picture.url(:medium) %>
You're trying to access the #post instance variable, but that instance variable doesn't exist. Rather, within your loop, you should access your post local variable as such:
<%= image_tag post.picture.url(:medium) %>
Note also that, from the documentation, the correct syntax to access variations of a Picture instance involves passing the variation name to the url attribute:
post.picture(:medium) # Invalid
post.picture.url(:medium) # Valid!
As mentioned by Jon, you need to use the .url method on your picture object (official documentation):
<%= image_tag post.picture.url(:medium) %>
Paperclip
The reason for this is Paperclip actually creates its own picture object & attaches several methods to that (url being one of them). This means you have to call url each time you show the post's image to get it to load
Normally, you'd be able to call methods on your instance or local variables, but as Paperclip actually creates its own object, you have to use its in-built methods to get it to work correctly
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>
I have a log in my webapp where you can input hours, and when you input your hours it automatically takes the account you are logged into(built using devise and cancan), and finds what your user_id is and tacks it on to an hour log.
Now what I cant seem to find out is how I can go about making it so that it only displays logs with your user_id. Is there a way to do this in the model or controller instead of the view?
This is the view code as of now.
<% #time_sheets.each do |time_sheet| %>
<tr>
<td><%= time_sheet.user_id %></td>
<td><%= time_sheet.day %></td>
<td><%= time_sheet.hours_worked %></td>
<td><%= time_sheet.minutes_worked %></td>
<td><%= link_to 'Show', time_sheet %></td>
<td><%= link_to 'Edit', edit_time_sheet_path(time_sheet) %></td>
<td><%= link_to 'Destroy', time_sheet, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
It shows the logs of everyone in the system instead of just that user.
Devise gives you a helper method named current_user. In your controller you can filter by user ID using the current_user.id. Like so:
#time_sheets = TimeSheet.where(:user_id => current_user.id).all
And that will give you only the currently logged in user's time sheets.