Prints out array when I didn't ask for it [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 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.

Related

Comments from post not displaying correctly in rails

Been trying to set up a regular blog in Ruby on Rails, and finally got the comment system to work within a post. However, when I loop through each comment and try to output the comment's title it displays this,
awef [#<Comment id: 6, title: "awef", link: nil, campaign_id: 5, user_id: 1, created_at: "2015-09-24 09:46:43", updated_at: "2015-09-24 09:46:43">]
Instead of just the title. How can I fix this?
The loop in your view should be something like this:
<% #blog.comments.each do |f| %>
<%= f.title %>
<% end %>
Please check you are using the right angular parenthesis (<%= %>) and not placing p puts or inspect commands inside them.
Edit:
Now that you show use the code, the problem is in the first angular parenthesis: should be <% not <%=. The first is for the logic, the latter to output erb code.
<% #blog.comments.each do |f| %> # remove "="
<%= f.title %>
<% end %>
<% ... %>: Executes the ruby code within the brackets
<%= ...%>: Executes the ruby code and show the executing result in webpage
Classic mistake, it's because you're using <%= ERB output tags for the .each loop. You can view more information on how to write correct ERB here.
You need to replace your <%= output tags with standard ERB tags <%:
<% #blog.comments.each do |f| %>
I was stung by that one myself when I was starting out.
The problem you have is probably the = in your each statement.
Try to use this:
<% #blog.comments.each do |f| %>
<%= f.title %>
<% end %>
Notice the removed = at <% #blog.comments.each do |f| %>
The reason is as the <%= %> always prints while <% %> only executes the code.

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.

How to concatenate items from loop [duplicate]

This question already has an answer here:
Rails separate array items with comma inside partial
(1 answer)
Closed 8 years ago.
I wonder if there's a quick way to concatenate the items from this view helper with a comma:
<% #projects.each do |project| %>
<%= link_to project.name, project_path(project) %>
<% end %>
so that I get:
Project name 1, Project name 2, Project name 3
Note that there's no comma at the end. Thanks for any ideas.
What about
<%= #projects.map { |p| link_to p.name, project_path(p) }.join(', ').html_safe %>
I added sanitize to p.name because otherwise if some special character is present in p.name it would break your HTML (and could lead to security issues, that is XSS attacks) I was wrong, I tried and it is escaped correctly.
<%= #projects.map {|project| link_to project.name, project_path(project)}.join(", ").html_safe %>

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| %>

Ruby on Rails App Displaying Unwanted Data [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
Whenever the following code runs i get extra output on the page which looks like the garbage on the bottom. I have tried to see if it is in my migrations or function calls, but they all seem to be pretty clean (not much too f.name). If needed I can provide a copy of my migrations and schema.
<%= #subject.Name %>
<%= #subject.formulas.each do |f| %>
<div class="entry">
<h3><%= f.name %></h3>
</div>
<% end %>
The extra output is:
[#<Formula id: 2, name: "Pythagorean Theorem", description: "n any right triangle, the area of the square whose ...", formula: "a^2+b^2=c^2", created_at: "2011-05-17 03:18:44", updated_at: "2011-05-17 03:18:44">]
Change
<%= #subject.formulas.each do |f| %>
to
<% #subject.formulas.each do |f| %>

Resources