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

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

Related

'Nil' error when calling models from root

I have a model called Museum and another model called Exhibtion. Museum is a parent of Exhibition.
I have created an adhoc controller in order to handle the root page.
routes file :
get 'welcome/index'
resources :museums
resources :museums do
resources :exhibitions do
resources :exhibitionimages
end
resources :museumimages
end
root 'welcome#index'
(please disregard museumimages and exhitionimages models)
I am trying to show all exhibitions on the root page as a table with links to show, edit, ...
here is the Welcome controller
class WelcomeController < ApplicationController
def index
#exhibitions = Exhibition.all
end
end
and the viewfile index.erb:
<h1>Hello, Rails!</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Start</th>
<th>End</th>
<th>Link</th>
<th>Price</th>
</tr>
<% #exhibitions.each do |exhibition| %>
<tr>
<td><%= exhibition.title %></td>
<td><%= exhibition.text %></td>
<td><%= exhibition.start %></td>
<td><%= exhibition.end %></td>
<td><%= exhibition.linktoexhibition %></td>
<td><%= exhibition.price %></td>
<td><%= link_to 'Show', museum_exhibition_path(exhibition.museum, exhibition), method: :get %></td>
<td><%= link_to 'Edit', edit_museum_exhibition_path(exhibition.museum, exhibition), method: :get %></td>
<td><%= button_to 'destroy', museum_exhibition_path(exhibition.museum, exhibition), method: :delete %></td>
</tr>
Though i get an error with the links.
It seems that the museum_id is not passed to the form link_to helpers where it appears as 'NIL' and fails the link creation.
Those links work perfectly when used inside the museum controller (where the same table is shown with exhibitions belonging to the very parent)

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.

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

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

Rails 3 - Nested Routes "NoMethodError" despite being listed in rake routes

Ok guys so I have a nested route like this:
resources :apps do
resources :forms
end
In my form index I have this block:
<% #forms.each do |form| %>
<tr>
<td><%= form.app_id %></td>
<td><%= form.title %></td>
<td><%= link_to 'Show', app_form(#app,form) %></td>
<td><%= link_to 'Destroy', form, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
The page throws a NoMethodError on the app_form line; however I think I am passing in the app and form in correctly (I've also tried to pass in the #app.id). Calling rake routes... the route is even displayed:
app_form GET /apps/:app_id/forms/:id(.:format) {:controller=>"forms", :action=>"show"}
Any help would be greatly appreciated!
Try app_form_path(#app, form) instead (you need to append _path to the route name).
Not only nested routes,For each routes you using, You need to append _path or _url with route name.
So here juz try app_form_path(#app,form) or app_form_url(#app,form)

Resources