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| %>
Related
This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I don't know why it is printing out an array. I used the same method for another checkbox and it doesn't print out an array.
This is the code for the view
<%= f.label "Category: " %>
<%= Category.all.each do |category| %>
<%= check_box_tag "course[category_ids][]", category.id %>
<%= category.cat_name %>
<% end %>
and this is what it prints:
Programming [#<Category id: 1, cat_name: "Programming", created_at: "2017-05-25 02:25:24", updated_at: "2017-05-25 02:25:24">]
Is there a way to remove the array?
You're printing Programming [#<Category id: 1, cat_name: "Programming"...] because you're using <%= ... %> instead <% ... %>, that's why you're printing the result of Category.all.each.
You can remove the = in your Category.all and that'll be as you want it.
Also I'd recommend you to create that as a variable in your controller and then to pass it to the view.
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.
I am working on a rails app and I have to models; Subs and Posts. Each post belongs to a sub, and a sub has many posts. In the Sub show view I want to list out all the titles of posts that it has.
Currently I have
<%= #sub.posts.each do |p|%>
<%= p.title %>
<% end %>
But when I run that it shows:
Test [#Post id:2, title: "Test", created_at: "2015-07-29 01:49:58",
updated_at: "2015-07-29 01:49:58", user_id: 1, sub_id: 1>]
I want it to just show Test, instead all the whole table entry
Thanks!
This is because you're using <%= in the line with the each statement, which means the result is outputted to the HTML, instead of <% which does not. The result of an each call is an enumerator object, which is the output you see after the title. The correct code would be:
<% #sub.posts.each do |p|%>
<%= p.title %>
<% end %>
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.
In my Rails app I'm trying to loop over the Submission instances inside my Folder instances with Rails templating code. It works. However, it's also returning each instance in code which doesn't seem to be JSON. It's what's returned when you look up an instance in the Rails console. Here's an example:
#<Submission id: 112, title: nil, content: nil, created_at: "2013-10-10 23:29:39", updated_at: "2013-10-10 23:29:39", user_id: 1, folder_id: 1, parent_id: nil>
Here's what the code looks like for the loop:
<%= #folder.submissions.each do |x| %>
<% if x.title != nil %>
<div id="<%= x.id %>" class="submission-textual">
<h1><%= x.title %></h1>
</div>
<% else %>
<% end %>
<% end %>
I checked my Folder and Submissions controllers but am not sure what this is. Why are these strings being rendered whenever I try and render an instance in my view? I'm still new to Ruby so that explains why I haven't seen this.
Try replacing the first line with
<% #folder.submissions.each do |x| %>
It's a small diffrerence, the equal sign after the first % was removed. I think that's what's causing the unwanted rendering.
The processing is as follows :
<% "ERB will evaluate this!" %>
<%= "ERB will evaluate and output this!" %>