Weird code appearing when I loop over model instances in Rails - ruby-on-rails

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

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.

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.

Show all the table entries that belong to a specific entry in Rails

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

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.

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