Doubled plus extra output, why? [duplicate] - ruby-on-rails

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I am getting doubled output on my page when the following code executes, what am I not seeing? I do not want the text inside the [].
<dt>Publications</dt>
<dd>
<ul>
<%= #person.pubs.each do |pub| %>
<li><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>, <%= pub.link %></li>
<% end %>
</ul>
</dd>
The output looks like the following:
Publications
Thiebaud N, Johnson MC, Butler JL, Bell GA, Ferguson KL, Fadool AR, Fadool JC, Gale AM, Gale DS, Fadool DA, Hyperlipidemic diet causes loss of olfactory sensory neurons, reduces olfactory discrimination, and disrupts odor-reversal learning., J. Neurosci., 2014,
[#<Pub id: 1000, person_id: 7, pubmed_id: nil, journal: "J. Neurosci.", title: "Hyperlipidemic diet causes loss of olfactory sensor...", auth_id: "Fadool DA", authors: "Thiebaud N, Johnson MC, Butler JL, Bell GA, Ferguso...", pages: "34(20):6970-84", date: "2014", type: nil, link: nil, notes: nil, created_at: nil, updated_at: nil>]
This text also includes information that is not being called in my code.

<%= #person.pubs.each do |pub| %>
<li><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>, <%= pub.link %></li>
<% end %>
should be
<% #person.pubs.each do |pub| %>
<li><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>, <%= pub.link %></li>
<% end %>
(notice there is no = sign on the line where you perform the .each loop).
<%= is a print statement. .each returns the collection, therefore the result in your case is a double output: one from the inner loop and one from .each.

Related

Rails simple_calendar rendering unwanted data

I am using simple_calendar in my Rails 5.0 application to link to events, everything works fine except that I get some extra data rendered in the calendar, I would like to get some help to figure it out.
This is what I have in the 'views/eventos/calendario.html.erb'
<h3>Eventos (<%= #eventos.count %>)</h3>
<%= month_calendar events: #eventos do |date, eventos| %>
<%= date.day %>
<% eventos.each do |evento| %>
<div>
<%= link_to evento.tipoEvento, evento %>
</div>
<% end %>
<% end %>
And this is what it is rendering for each day with programmed events:
<td class="day wday-5 past current-month has-events">
9
<div>
Entrenamiento
</div>
[#<Evento id: 4, fecha: "2016-12-09", tipoEvento:
"Entrenamiento", equipo_id: 11, comment: "Cancha 2",
created_at: "2016-12-08 06:07:19", updated_at: "2016-12-08
06:22:03", registrado: true>]
</td>
For days without events it is rendering the empty []. See, it is rendering the correct tag but I don't know how to avoid it rendering the object data, Can somebody help me please?
The solution is to replace a line in the generated view: views/simple_calendar/_month_calendar.html.erb
from:
<%= block.call day, sorted_events.fetch(day, []) %>
to:
<% block.call day, sorted_events.fetch(day, []) %>
So Jagdeep's answer was right, but in a different code line.

How to stop this array of attributes from showing in my view? [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I have an annoying problem where my view keeps displaying an array of my object's attributes! My submenu is suppose to show categories in a tree form, which works, but it also shows this annoying array!
[ <#Category id: 26, title: "subtest", description: "a test within a test, testception", created_at: "2015-03-01 03:15:29", updated_at: "2015-03-03 01:08:09", ancestry: "6/24">]
[ <#Category id: 24, title: "Test", description: "No not be alarmed, this is only a test.", created_at: "2015-03-01 02:06:35", updated_at: "2015-03-03 01:07:52", ancestry: "6">]
I definately don't want the user to see this. How do I get rid of it??
Show.html.erb view:
<div id="submenu">
<%= render 'submenu_cats', categories: #category.root.children %>
</div>
_submenu cats partial:
<ul>
<%= categories.each do |category| %>
<li>
<%= link_to_unless_current category.title, category_path %>
<%= render 'submenu_cats', categories: category.children if category.children.present? %>
</li>
<% end %>
Using: Rails 4.2, Ruby 2.1.5, Ancestry Gem
You are using <%= %> which means that you are outputting the results of a Ruby code, instead use <% %> to executes the ruby code within the brackets.
So change
<%= categories.each do |category| %>
To
<% categories.each do |category| %>
Hope this help. :)
Don't use...
<%= categories.each do |category| %>
Use
<% categories.each do |category| %>
When you use <%=, you're outputting the result of the expression. The result of categories.each is categories, the array that is being output. You don't want to output it, so use <% to evaluate the Ruby without outputting the results.

When using rails Action View Helpers - how can you customize the params to be an Array of the labels?

I have this view
...code....
<% #feeds.each do |feed| %>
<%= check_box_tag(feed.name) %>
<%= label_tag(feed.name) %>
<% end %>
...code....
The Feed model looks like this
Feed(id: integer, name: string, description: string, url: string, created_at: datetime, updated_at: datetime, day_selector: string, special_selector: string)
and the submission comes into the params hash like this
{"utf8"=>"✓",
"authenticity_token"=>"WVbxZckIJCA0dXqPZGnSXJi7yrDN3Ssttv7dnJZOfBY=",
"email"=>"",
"phone_number"=>"",
"Squeaky Beaker"=>"1",
"Commonwealth Cambridge"=>"1",
"commit"=>"GO",
"action"=>"create",
"controller"=>"subscriptions"}
I want the params hash to look this this
{:feeds => {'Squeaky Beaker' => 1, 'Commonwealth Cambridge' => 1}}
or just simply
[{'Squeaky Beaker' => 1, 'Commonwealth Cambridge' => 1}]
How can I customize my view to have the params hash look the way I want?
untested, but you could try...
<ul>
<% #feeds.each do |feed| %>
<li>
<%= check_box_tag 'feeds[]', (feed.name => 1) -%>
<%= h feed.name -%>
</li>
<% end %>
</ul>
I'm not sure the => 1 has value to your application, so the more traditional approach would be
<%= check_box_tag 'feeds[]', feed.name -%>
interpreted from the documentation at http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag
Check Box
To further SteveTurczyn's answer, what you're basically doing right now is outputting two checkboxes (completely different to each other), which will both hold a value of "1" (checked):
<% #feeds.each do |feed| %>
<%= check_box_tag(feed.name) #-> feed.name will be different each time %>
<%= label_tag(feed.name) %>
<% end %>
You'll need to give the check boxes the same name, as to give Rails the ability to discern their values being different. And secondly, you'll need to ensure you have the correct vales / options for the boxes too:
<% #feeds.each do |feed| %>
<%= check_box_tag "feeds[]", feed.name %>
<% end %>
This will pass the parameters as follows:
["checked_value_1", "checked_value_2"]
If you use the names of the feeds, it will give you:
["Feed1", "Feed2"] #-> names
["5", "6", "7" ] #-> ids

Why is Rails displaying all the information on the page? [duplicate]

This question already has answers here:
Why am I getting objects printed twice?
(4 answers)
Closed 6 years ago.
I have the following code in my rails 3.2 app:
<ul>
<% user_to_show.topics.each do |topic| %>
<li> <%= link_to topic.name, topic %>
<ul>
<%= topic.nodes.each do |node| %>
<li> <%= link_to node.title, node %> </li>
<% end %>
</ul>
</li>
<% end %>
</ul>
When I go to a user page, I would expect it to just display a link to each topic and the nodes in that topic. It does that, but then it also displays the actual data of each node at the end (see below). How come and how do I fix it?
Sample Topic
node1
node2
[#<Node id: 1, title: "node1", intro_content: "sample content", user_id: 2, date_modified: nil, created_at: "2012-07-19.." [etc..] #<Node id: 2, title: "node2", intro_content: "sample 2 content..", user_id: 2, etc.. ]
Because of this line:
<%= topic.nodes.each do |node| %>
Every Ruby statement is an expression. So every method returns something (even if it's nil). So if you call an "each", it will return the Array after the loop is done. So, as you used <%=, it will write this array after the loop is over.
You should have used:
<% topic.nodes.each do |node| %>

iterate through active record results - ruby on rails

I've been trying got find this answer, and maybe it's too simple...
In rails, how is the best way to iterate through results from activerecord pull the specific fields that you want?
I have a controller for comments (named posts) that pulls all records:
def index
#posts = Post.find(:all)
end
Then in the index view, when I use <%= #posts %> I get all of the data...which is great...
#<Post id: 1, user_id: "9", picture: nil, comments: "here's a first comment", title: nil, twitter: nl, frame: nil, created_at: "2012-05-09 04:21:16", updated_at: "2012-05-09 04:21:16"> #<Post id: 2, user_id: "9", picture: nil, comments: "here's a second comment", title: nil, twitter: "please", frame: nil, created_at: "2012-05-09 05:20:03", updated_at: "2012-05-09 05:20:03">
How can I now iterate through test so that the view shows the data from the comments and created_at fields:
Here's the first comment, 2012-05-09 04:21:16
Here's the second comment, 2012-05-09 05:20:03
I've tried the following and get an error.
<% #posts.each do |c| %>
<%= c.posts.comments %>
<%= c.posts.created_at %>
<% end %>
The "c" in #posts.each do |c| represents the specific post object in the #posts collection.
So, in a sense you are trying to do <%= post.posts.comments %>.
Here's how the code should look:
<% #posts.each do |p| %>
<%= p.comments %>
<%= p.created_at %>
<% end %>
Change things to this:
<% #posts.each do |post| %>
<%= post.comments %>
<%= post.created_at %>
<% end %>
I find it makes it easier for people to follow if you name the inner variable as the singular of the out variable -- therefore #posts on the outside becomes post on the inside.
Good luck!

Resources