Rails 3 Routing Error with Nested Resources - ruby-on-rails

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) %>

Related

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.

rails "No route matches" even though it's in routes

I have this form:
<% #softwares.each do |l| %>
<tr>
<td><%= l.vendor %></td>
<td><%= l.title %></td>
<td><%= l.edition %></td>
<td><%= l.amount %></td>
<td><%= link_to 'view', software_path %></td>
<% end %>
When i click on the view link i get this error:
No route matches {:action=>"show", :controller=>"softwares"}
However when i run rake routes it does show up:
software GET /softwares/:id(.:format) softwares#show
and if i type it into the browser manually it works fine
Pass software object in path because it's a member route
<%= link_to 'view', software_path(l) %>
For RESTful resources you can just pass the resource:
link_to 'view', l
# => view

ActionController::RoutingError in Bank_accounts#index

I seem to be getting a routing error within my Rails project each time I try access the index for "Bank Accounts". I'm quite new to Rails so this should probably be a simple error. If I've missed anything I apologise.
Errors I'm getting:
ActionController::RoutingError in Bank_accounts#index
Showing app/views/bank_accounts/index.html.erb where line #21 raised
This is the line of code that Rails doesn't seem to like:
<td><%= link_to 'Transaction Details', bank_account_transaction_path(bank_account) %> </td>
Routes file:
ActionController::Routing::Routes.draw do |map|
map.resources :bank_accounts, :has_many => [:transactions]
map.root :controller => "bank_accounts"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Index view for Bank Accounts
<% #bank_accounts.each do |bank_account| %>
<tr>
<td><%=h bank_account.account_number %></td>
<td><%=h bank_account.holders_name %></td>
<td><%=h bank_account.overdraft_limit %></td>
<td><%=h bank_account.current_balance %></td>
<td><%=h bank_account.active %></td>
<td><%= link_to 'Show', bank_account %></td>
<td><%= link_to 'Edit', edit_bank_account_path(bank_account) %></td>
<td><%= link_to 'Transaction Details', bank_account_transaction_path(bank_account) %> </td>
<td><%= link_to 'Destroy', bank_account, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
My understanding of the Rails naming convention is that if you want to link to an index of nested resources, you need to use the plural:
bank_account_transactions_path(bank_account)
And if you want to link to a particular nested resource, use the singular and pass in the nested resource ID as a second argument:
bank_account_transaction_path(bank_account, txnid)
And this question has an example of a cleaner syntax - you might like that better.
hope that helps!
You should
Resources :bank_accounts do
member 'transaction', :method=> :get
End
In your index
transaction_bank_account_path(account)
Controller
Def transaction
....
End

Cannot get added Method and link_to to work (Rail 3)

Seen some references, but its old Rails 2 solutions. Having a hard enough time understanding some of the Rails 3 nomenclature.
I added a method 'dndl' in my controller.
I added a link_to in my index.
I TRIED and TRIED again to put routes in.
Controller:
def dnld
blah blah
end
Index:
<td><%= link_to 'Show', stock %></td>
<td><%= link_to 'Edit', edit_stock_path(stock) %></td>
<td><%= link_to 'Dnld', dnld, {:action => 'dnld'} %></td>
<td><%= link_to 'Destroy', stock, :confirm => 'Are you sure?', :method => :delete %>
Routes:
resources :stocks do
collection do
put 'dnld'
end
end
I've tried:
<td><%= link_to 'Dnld', stock, {:action => 'dnld'} %></td>
# End up on the stock show page with dnld not executed to my knowledge
<td><%= link_to 'Dnld', , {:action => 'dnld'} %></td>
# Produces an error
<td><%= link_to 'Dnld', dnld_stock_path(stock), {:action => 'dnld'} %></td>
# It doesn't know what dnld_stock_path is, yet I don't understand why it DOES know what edit_stock_path is and cannot find documentation to explain this.
Thanks for the help!
If you are giving collection in your routes.rb like:
resources :stocks do
collection do
put 'dnld'
end
end
then the named path will be 'dnld_stocks_path'. And you don't need to specify the action.
<td><%= link_to 'Dnld', dnld_stocks_path %></td>
If you are giving member in your routes.rb like:
resources :stocks do
member do
put 'dnld'
end
end
then the named path will be 'dnld_stock_path(stock)'.
<td><%= link_to 'Dnld', dnld_stock_path(stock) %></td>
For more info visit
Try
resources :stocks do
collection do
put :dnld, :as => dnld
end
end
then
<%= link_to "Dnld", dnld_controllername_path %>

Resources