Append pdf extension to link - ruby-on-rails

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>

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>

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.

Only displaying certain entries from a log based on id

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.

Ruby On Rails strange routes path

I have this routes in my app (rails 3.2):
godmode_invites GET /godmode/invites(.:format) godmode/invites#index
POST /godmode/invites(.:format) godmode/invites#create
new_godmode_invite GET /godmode/invites/new(.:format) godmode/invites#new
edit_godmode_invite GET /godmode/invites/:id/edit(.:format) godmode/invites#edit
godmode_invite GET /godmode/invites/:id(.:format) godmode/invites#show
PUT /godmode/invites/:id(.:format) godmode/invites#update
DELETE /godmode/invites/:id(.:format) godmode/invites#destroy
And in template view:
<td><%= link_to 'Show', godmode_invites_path(invite) %></td>
<td><%= link_to 'Destroy', godmode_invites_path(invite), method: :delete, data: { confirm: 'Are you sure?' } %></td>
This produces strange routes like with point before resource ID:
/godmode/invites.3
/godmode/invites.4
I can not find my problem...
There is little error your in view. Here is the corrected code:
<td><%= link_to 'Show', godmode_invite_path(invite) %></td>
<td><%= link_to 'Destroy', godmode_invite_path(invite), method: :delete, data: { confirm: 'Are you sure?' } %></td>

Rails 3 Routing Error with Nested Resources

In my Rails application, there are many games, and each game has it's own set of leaderboards. It makes sense then, to have the leaderboards nested in the game, so you can only get to a leaderboard through a game. I setup my routes.rb file as such (the important part):
resources :games do
resources :leaderboards
end
So then I updated my controller so it would get the appropriate game from the game_id passed in, and grab the leaderboard information from that. However, my issues comes from my view. In this section (auto generated view from the scaffold):
<% #leaderboards.each do |leaderboard| %>
<tr>
<td><%= leaderboard.name %></td>
<td><%= leaderboard.scoreColumnName %></td>
<td><%= leaderboard.game_id %></td>
<td><%= link_to 'Show', [#game, leaderboard] %></td>
<td><%= link_to 'Edit', edit_game_leaderboard_path(leaderboard) %></td>
<td><%= link_to 'Destroy', [#game, leaderboard], :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
The code breaks saying:
No route matches {:action=>"edit", :controller=>"leaderboards", :game_id=>#<Leaderboard id: 1, name: "Test High Score Leaderboard", scoreColumnName: "Score", game_id: 1, created_at: "2011-07-03 01:32:33", updated_at: "2011-07-03 01:32:33">}
This line, it turns out is the error: (line 19 in my code)
<td><%= link_to 'Edit', edit_game_leaderboard_path(leaderboard) %></td>
Removing this line, and the view renders fine. So, the URL part is broken, but how do I fix it? The weird thing is, I have that exact "edit_game_leaderboard_path" in the Show view, and it works fine... what am I doing wrong?
You want:
<%= link_to 'Edit', edit_game_leaderboard_path(#game, leaderboard) %>

Resources