Only displaying certain entries from a log based on id - ruby-on-rails

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.

Related

Rails link_to not working correctly

I am trying to put in a link_to on my table to go to the show action but it is putting the URL as /admin/vulnerabilities.object_id instead of /admin/vulnerabilities/object_id
my index view is:
...
<% #vulnerabilities.each do |vulnerability| %>
<tr>
<td><%=link_to vulnerability.id, admin_vulnerabilities_path(vulnerability) %></td>
<td><%= vulnerability.type %></td>
<td><%=h truncate(vulnerability.description, :length => 80) %></td>
<td><%= vulnerability.published %></td>
<td><%= vulnerability.modified %></td>
<td><%= link_to vulnerability.href, vulnerability.href , target: :_blank %></td>
</tr>
<% end %>
...
I have a show.html.erb template setup and my show action is as follows:
def show
#vulnerabilities = Vulnerability.find(params[:id])
end
From what I can see, this should work but when clicking the links it just redirects to the index page, effectively refreshing it and not using my show page at all.
It would be helpful if you added the relevant parts of your routes.rb file to your question, but I speculate that the problem is that admin_vulnerabilities_path(vulnerability) should be admin_vulnerability_path(vulnerability).
Also, as noted in the comments, it is probably better to use #vulnerability as your instance name since find will return a single record.

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>

Rails 4 error: The scope body needs to be callable

I'm trying to make an app in Rails 4.
I have an industry model.
I'm trying to make an index of industries, and list them alphabetically.
I have an industry.rb with:
scope :alphabetically, order("sector ASC")
I have an index controller with:
def index
#industries = Industry.alphabetically
end
In my index view, I have:
<% #industries.each do |industry| %>
<tr>
<td><%= image_tag industry.icon.tiny.url %></td>
<td><%= industry.sector %></td>
<td><%= link_to 'Show', industry %></td>
<td><%= link_to 'Edit', edit_industry_path(industry) %></td>
<td><%= link_to 'Destroy', industry, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
When I try this, I get this error:
ArgumentError in IndustriesController#index
The scope body needs to be callable.
How do I make a scope 'callable'?
As per the error message, the scope's body needs to be wrapped within something callable like a Proc or Lambda. Like this:
scope :alphabetically, -> {
order("sector ASC")
}
This ensures the contents of the block is evaluated each time the scope is being used.
So, if you change your scope as shown above, it should work and fix your problem.

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.

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