in my route.rb I have this
Rails.application.routes.draw do
resources :cars do
resource :payments
end
end
However, in my destroy link for payments. the URL generated is
http://localhost:3000/cars/9/payments.11
Below is my code.
<% #car.payments.each do |p| %>
<tr>
<td><%= p.date %></td>
<td><%= p.profit %></td>
<td><%= p.remark %></td>
<td><%= link_to 'Delete', car_payments_path(#car, #p) ,
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Please advice. Thank you in advanced.
Looks like this is a pluralization error.
Try
cars_payment_path
instead of
car_payments_path
To delete a payment in a car, the route should be a member route , call it like this:
car_payment_path(#car, #p)
car_payments_path(..) was a collection route of payments.
Suggest you to test at console like this:
app.car_payment_path(Car.first, Car.first.payments.first)
Related
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.
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 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
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)
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 %>