Cannot get added Method and link_to to work (Rail 3) - ruby-on-rails

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

Related

Passing parameters using link_to

I'm trying to pass parameters using link_to with ruby on rails, but it says the id parameter I'm sending is null.
code from where I'm sending the id.
<% #conference.papers.each do |paper| %>
<tr>
<td><%= paper.title %></td>
<td><%= paper.author %></td>
<td><%= link_to "Download Paper", paper.attachment_url %></td>
<td><%= link_to 'Reviews', paper %></td>
<% if (paper.accepted) %>
<td><%= "Accepted" %></td>
<% else %>
<td><%= "Not accepted" %></td>
<% end %>
<% if (#state1 && paper.accepted == false) %>
<td><%= button_to "Accept", accept_paper_path(id: paper.id), class: "btn btn-danger", data: { confirm: "Are you sure that you wish to accept #{paper.title}?"} %></td>
<% end %>
<% if (#state2) %>
<% session["a"] = paper.id %>
<td><%= link_to "Review paper", new_review_path(id: paper) %></td>
<% end %>
</tr>
<% end %>
code for the review controller
def new
#paper = Paper.find_by_id(params[:id])
#review = Review.new()
end
You missed .id in
link_to "Review paper", new_review_path(id: paper.id)
But it is not a good solution. If your Paper model has_many :reviews, it would be better to nest reviews routes in paper's ones. Like this:
# config/routes.rb
resources :papers do
resources :reviews
end
And so, your link_to will look like:
link_to "Review paper", new_paper_review_path(paper)
which will generate
/papers/:paper_id/reviews/new
You can learn more about Rails routing here.
Lets start by setting up the routes properly:
resouces :papers do
member do
patch :accept
end
end
This will let you accept a review by PATCH /papers/:id. To create the button use:
<%= button_to accept_paper_path(paper), method: :patch %>
Note that this should use the PATCH or PUT http method - not GET since it is a non-idempotent action.
Note that you can just pass the model instead of doing accept_paper_path(id: model) or accept_paper_path(id: model.id).
For reviews you will want to create what is called a nested resource:
resouces :papers do
member do
patch :accept
end
resources :reviews, only: [:new, :create]
end
This gives you the route /papers/:paper_id/reviews/new.
<%= link_to "Review paper", new_paper_review_path(paper) %>
To set the form to create a new review to the use correct path use an array containing the parent and child:
<%= form_for([#paper, #review]) %>

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.

URL link gives dot instead of slash

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)

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