Index View Error wrong amount of arguments - ruby-on-rails

I'm getting an argument error and I am not to sure why. I am still fairly new to using Rails and programming overall. I am currently working on my index page and am just trying to get the description displayed. Below is the view and controller. I am not sure why t.description is being expected to have more than one argument. Is this because of strong params?
View:
<h1> All Tea Blends </h1>
<% #teas.each do |t| %>
<h2><%= link_to t.flavor, tea_path(t.id)%> - <%= t.brand.name %></h2>
<% link_to "Write a review", new_tea_review_path%>
<% end %>
<div>
<p><%= t.description %></p>
</div>
Controller:
class TeasController < ApplicationController
def index
#teas = Tea.order_by_rating.includes(:brand)
end
end

t.description is outside of the loop. Not sure why you'd get wrong number of arguments (t is also used for translations, though). Just move it inside the loop...
<% #teas.each do |t| %>
<h2><%= link_to t.flavor, tea_path(t.id) %> - <%= t.brand.name %></h2>
<% link_to "Write a review", new_tea_review_path %>
<div>
<p><%= t.description %></p>
</div>
<% end %>
I'd also recommed changing your loop variable to tea instead of t. Its more descriptive and avoids conflicts.

Related

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

How to specify syntax error in Ruby on Rails

My controller is like this
def show
#receipt = Receipt.find(params[:id])
#hospitalizations=#receipt.hospitalizations
#outpatients=#receipt.outpatients
#surgeries=#receipt.surgeries
end
my show.html.erb is like this.
<h1>details of receiptid: <%= #receipt.id %></h1>
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% #surgeries.each do |surgery| %>
<p>surgeryid:<%= surgery.id %> surgery_day :<%= surgery.surgery_day %> </p>
<% #outpatients.each do |outpatient| %>
<p>outpatientid:<%= outpatient.id %>outpatient_day :<%= outpatient.outpatient_day %> </p>
<% end %>
When I access it's page,some error like below was incurred. I tried to specify error location,but didn't work well.
It seems syntax error,where should end insert?
If someone has experienced same issues,please let me know.
Each of your each loop requires an <% end %> clause, you can't find the specific line since the error says it reached the end when it expected "end". Close all your loops like this:
<% #hospitalizations.each do |hospitalization| %>
<p>hospitalization_id:<%= hospitalization.id %>hospitalization_start :<%= hospitalization.hospitalization_start %> hospitalization_end:<%= hospitalization.hospitalization_end %> </p>
<% end %>

Rails link_to_if not hiding links correctly

For some reason my link_to_if line works, but appears on every show view for my model (Company).
Here's the code:
<% #customers.each do |customer| %>
<li>
<%= link_to_if customer.company_id == #company.id, "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>
The issue: I have Customer1 linked to CompanyX. When I go to CompanyZ it shows Customer1, but the link is not a hyperlink. it's just plaintext, and not even supposed to be showing up. However on CompanyX's view, the link works fine. What am I doing wrong here?
If you read the documentation of link_to_if (https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if), it clearly says that [if false] only the name is returned.
In the doc you can find that the (optional) block given is rendered in the false case. So in your case you could pass it an empty block:
<%= link_to_if false, customer_path(customer[:id]) {} %>
In my opinion, if you want to display the link only if one or more customer(s) from #customers are associated to that #company, you should do it this way:
<% #customers.where(company_id: #company.id).each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% %>
if you want to hide some records you can do from from controller to control customers based company
#customers = Company.find(:id).customers
then in your views you can just show it without to compare it
<% #customers.each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>

Ruby on Rails how to get last query?

I'm having trouble trying to figure out when I reached the end of my query. So what I want to do is list all the records in my database that begin with the letter A which I got however I want to output a message if the query turns out blank. When I try I get a bunch of my custom messages even the query didn't turn out blank. Is there any way to tell if I've reached EOF in ruby on rails?
Sample
<div id = "content-A">
<p>A</p>
<% #animes.each do |anime| %>
<% if anime.aname.starts_with?('A') %>
<%= link_to anime.aname, {:action => 'list'} %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<%end %>
</div>
I believe you want sth like:
<% animes_group = #animes.group_by {|anime| anime.aname.to_s[0].upcase}
('A'..'Z').each do |letter| %>
<div id="content-<%= letter %>">
<p><%= letter %></p>
<% if animes = animes_group[letter] %>
<% animes.each do |anime| %>
<%= link_to anime.aname, {:action => 'list'} %>
<% end %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<% end %>
You should consider moving some of the logic to the controller here, however what is to be moved depends on many factors like whether #animes are being used anywhere else etc.

simple rails helper gives "NoMethodError"

helpers/subcategories_helper.rb:
module SubcategoriesHelper
def has_topic_headings?
self.topic_headings
end
end
categories/show.html.erb contains
<% #category.subcategories.each do |subcategory| %>
<li>
<h6>
<%if subcategory.has_topic_headings? %>
<%= link_to subcategory.name, subcategory, data: :has_topic_headings %>
<% else %>
<%= link_to subcategory.name, subcategory %>
<% end %>
</h6>
<hr>
</li>
<% end %>
The page returns
undefined method `has_topic_headings?' for #<Subcategory:0xa68748c>
Please note that the view page belongs to the Category, not the Subcategory.
You are trying to call it on model which is why it is not called on when helper is included. Helpers are there for views and sometimes for controllers.
Here is your method :
<%= link_to subcategory.name, subcategory, data: :has_topic_headings %>
Edit: Sorry for misunderstanding. the error is in your data which you are passing in your link:
data: :has_topic_headings
rails is asking for above method, not for has_topic_headings?
Edit: Yes, as #techvineet said, you cannot call a helper method on a subcategory object.
you should write method in your subcategory model:
def has_topic_headings?
return true if self.topic_headings
end
Or you can do this in your helper class:
def has_topic_headings(subcategory)
return true if subcategory.topic_headings
end
and in your form, call it like this:
<%if has_topic_headings(subcategory) %>
<%= link_to subcategory.name, subcategory, data: :has_topic_headings %>
<% else %>
<%= link_to subcategory.name, subcategory %>
<% end %>
Hope it will help.Thanks

Resources