Using link_to to trigger an increment action in a controller - ruby-on-rails

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.

Related

How can i get User name in a table where user_id is used as foreign key

I am using Devise gem for user Authentication. User id is as foreign key in article table
How can i get writer name through User_id in a view Show_article.html.erb
I can access user_id in show_article.htmlerb
I have tried to make a custom function in article controller but could not get the desired output
your Article model should look like this:
class Article << ActiveRecord::Base
belongs_to :user
#....some more lines
end
your User model should look like this:
class User << ActiveRecord::Base
has_many :articles
#....some more lines
end
and your show.html.erb should say:
#....your rest of code
<%= #article.try(:user).try(:name) %>
#....your rest of code
This will skip user name if you article doesn't have any user. It looks like your article doesn't have user_id or you haven't defined your relations correctly.
It might be the case that some of your article do not have any user. so
you can do
<%= #article&.user&.name %> in show page
and in the index page
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article&.user&.name%></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure you want to delete this article?' } %></td>
</tr>
<% end %>
but make sure you have ruby 2.3.0 or higher

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)

Unable to find "Destroy" link

I am trying to write test scenario for delete,but i don't understand why it is not getting destroy link.
Here my test scenario:
Scenario: User can delete kids
Given I am on the kids page
When I Destroy kid
Then I should see "Kid deleted successfully"
Then one kid should not exist
<h1>Listing kids</h1>
<tr>
<th>Kid name</th>
<th colspan=3>Action</th>
</tr>
<% #kids.each do |kid| %>
<tr>
<td><%= kid.kid_name %></td>
<td><%= link_to 'Show', kid %></td>
<td><%= link_to 'Edit', edit_kid_path(kid) %></td>
<td><%= link_to 'Destroy', kid, method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'New Kid', new_kid_path %>
My step defination for link:
When /^I Destroy kid$/ do |link|
click_link(link)
end
Please somebody suggest step definition for delete link, please correct me if their is some error in my scenario.
Thank you.
I have found the answer for destroy link. It won't support webrat so i have removed webrat and used capybara and my issue get solved.
Here is the step:
Scenario: Delete Kid
Given I am on the kids page
And there is a kid with kid_name "john"
When I destroy that kid
Then I am on the kids page
step_defination:
When /^I destroy that (.*)$/ do |element_type|
element = element_type.classify.constantize.last
path = "#{element_type}_path"
case page.driver
when Capybara::RackTest::Driver
page.driver.submit :delete, send(path, element), {}
else
visit send(path, element, { method: :delete })
end
end </code>

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