I am trying to display the number of followers a user has.
My original code was <h5><%= current_user.followers.count %> followers<h5>
This produces the incorrect statement: "1 followers"
How to pluralize this sentence? I experimented with the following and I can't get it to work.
<%= 'follower'.pluralize(current_user.followers.count) %>
This displays just the word "follower" and no number in front of it.
Try
<%= pluralize(current_user.followers.count, 'follower') %>
See http://apidock.com/rails/ActionView/Helpers/TextHelper/pluralize
Related
I need to parse a local HTML file using Nokogiri, but the HTML doesn't have any <div>s with classes. It starts with text.
This is the HTML:
high prices in Example 1<br>
low prices in Example 2<br>
In this case I just need to get "high" and "low", and "example 1", and "example 2".
How can I get the text, with no elements? From the tutorials I saw, it needs some <div class= ...> to get the text.
doc.xpath('//a/#href').each do |node| #get performance indicators
link = node.text
#test << Entry2.new(link)
end
#title = doc.xpath('//p').text.scan(/^(high|low)/)
My view:
<% #test.each do |entry| %>
<p> <%= entry.link %></p>
<% end %>
<% #title.each do |f| %>
<p> <%= f %></p>
<% end %>
And the output is like this:
Example 1Example 2
[["high"], ["low"]]
It's listing all at the same time instead of one by one. How can I change my Nokogiri code to look like this in the output?
high prices in Example 1
low prices in Example 2
Well, Nokogiri will wrap that string in an implicit <html><body><p>... so the text will be in a single <p>
So yes, you will be able to get the links in a structured form with:
doc.xpath "//a"
The "high" and "low" strings will be in a single blob of text. You will probably need to pull them out with some regex which will depend a lot on your requirements and data, but here's the regex for what you're showing and asking for:
doc.xpath('//p').text.scan(/^(high|low)/)
I can't be sure how helpful that will specifically be with your actual requirements, but hopefully it gives you a direction to take.
I'm very new to Ruby and am attempting to display one of two messages based on whether certain words are included in an array. My current controller contains the following:
#cookie = ["gluten", "sugar", "dairy", "chocolate"]
And my view contains this:
<%= #cookie.include?"gluten" %>
The above returns 'true' and prints on the page just fine. However, nothing gets printed on the page when trying either of the following methods. The page renders fine with no errors, but no messages:
<%= puts "Sorry" if #cookie.include?"gluten" %>
and
<%=
if #cookie.include?("gluten")
puts "Sorry, you cannot eat this."
else
puts "You have the greenlight."
end
%>
I'm hoping that I'm creating a very simple mistake in syntax or am misunderstanding the usage of the include? function.
looking at the ruby it appears to be sound, http://rubyfiddle.com/riddles/d6798.
Must be rails error so try
<% if #cookie.include?("gluten") %>
<%= "Sorry, you cannot eat this." %>
<% else %>
<%= "You have the greenlight." %>
<% end %>
If you want it all in one erb statement, you would do something like this:
Don't use "puts". The "<%=" is already doing a 'to_s' and will output the value to the response body. You may have seen the results show up in your logs if you used 'puts'.
Also - for erb, the <% ... %> syntax, that is for logic, often an 'each' or 'if' state, the <%= %> is for writing to the screen. Most of the time it is the attribute of a ruby object like <%= user.name %>
I am creating a section of my Rails application for the visually impaired. This requires me to create it using only text and links in order to make it easier for people using speech readers to navigate through. I want to use fields from an existing model to dynamically build a link_to command. I would like to be able to build a variable using several fields on the model that contains the text that a user clicks on and another field from the model which contains the link.
Here is the code in my controller:
MediaLibrary.find(:all, conditions: ["media_type_id < ?", 3], limit: 5).each do |media_item|
#audio_links["link_text"] = "Audio of #{MediaCreator.find(media_item.media_creator_id).name} #{media_item.media_created.to_time.strftime('%A, %B %d, %Y')} at #{media_item.meeting_time}
#{media_item.am_pm} - #{media_item.name}"
#audio_links["link"] = media_item.link
end
Here is the code in my view:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to audio_link["link_text"], '#{audio_link["link"]}' %>
</li>
<% end %>
I have also tried the following:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to 'audio_link["link_text"]', '#{audio_link["link"]}' %>
</li>
<% end %>
And this:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to '#{audio_link["link_text"]}', '#{audio_link["link"]}' %>
</li>
<% end %>
I have tried a few more variations but I either get the can't convert String into Integer error on the link_to command when I attempt to display the screen or the links display with the text being displayed as the following. When this happens I get other errors when I click the link.
#{audio_link["link_text"]}
I have done a lot of searches on Stack Overflow and throughout the web. I have not found a single example of this being done anywhere. I have seen in older posts where there was a set_path command (2010) but nothing for recent posts. I have used html_safe! before and will add that to my code. I do not know if there is a problem with my code or if I am attempting something that is not possible. I sincerely hope this is possible because it will make it easier for people with speech readers to know what they are clicking on.
Any help with this would be appreciated.
You can't do string interpolation in single quotes. Replace the single quotes with double quotes and your variables will be expanded properly.
For example:
<%= link_to audio_link["link_text"], "#{audio_link['link']}" %>
I'm trying to left justify a string in a rails view with a pad character.
The following doesn't work:
<%= menu_items.description.ljust(5,".")%>
Neither does this:
<%= menu_items.description.to_s.ljust(5,".")%>
Just to mess around and try to get something I found the following works.
<%= menu_items.description.length.to_s.ljust(5,".")%>
It prints out the length converted to a string and appends the pad characters. What gives? How do I make the first snippet work?
The following works because .length gives you a number, probably zero, and then adds ....
<%= menu_items.description.length.to_s.ljust(5,".")%>
What do you get when you just do this:
<%= menu_items.description %>
Look like menu_items is an array. Try this
<% menu_items.each do |menu_item| %>
<%= menu_item.description.ljust(5,".") %>
<% end %>
Also, if it is not an array, then the .. will be appended to make it a total length of 5. If description is longer, it will do nothing.
Am looping through a set of records and would like to display the model's associated data at the same time.
However when trying to display the associated data, am getting an error message
Can anyone tell me What's wrong with the following code?
<% #subs.each do |submission| %>
<%= submission.SUB_OID %>
<%= submission.SUB_ASSAY_TYPE %>
<%= submission.author.AUT_NAME %> -- am getting the error because of this line
<% end %>
Model:
Sub has_one Author
Author belongs_to Sub
If i remove this line <%= submission.author.AUT_NAME %>, the list of submissions are displayed properly, however when i include the 3rd line, i get the error 'Undefines method for AUT_NAME'.
I don't understand where the error is found.
Am a newbie and would be grateful for any suggestion provided
Either your author has no field/method named AUT_NAME or your relationship is wrong. Can you check if submission.author is nil and make sure AUT_NAME exists?
Finally found the solution. I included the following if condition --
<% if submission.author %>
<%= submission.author.AUT_NAME %>
<% end %>
The reason why author name couldn't be displayed was because not all submissions have an associated entry in the authors table.