why is Rails outputting my array?
Change <%= @employees.each do |empl| %>
to <%- @employees.each do |empl| %>
and it'll stop outputting the list. That is, change the =
to a -
. Using the =
tells ERB to output the result of that command. When you call each
, it returns the list so the list is getting output.
As for your unless
, I don't think it's actually necessary. If the list is empty?
then each will just return without actually performing any repetitions, so the code inside the loop will never get called.
Why is this rails view spitting out a raw array at the end of an .each do loop?
Try removing the equals sign in <%= @post.comments.each do |comment| %>
. The equals is only necessary if the method itself outputs something. In this case you're just using it to iterate a collection.
Unexpected array output on page
<% %>
Executes the Ruby code inside
<%= %>
Prints the results
You are displaying array and then it's values, so you would want to change <%= %>
to <% %>
.
<%= @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
to
<% @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
How to stop this array of attributes from showing in my view?
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.
Rails Console outputting only first record of array?
As you can see c.products
didn’t trigger sql query, that means it is cached. c.products.count
on the other hand triggers a query and tell you actual information from database. So your c.products
possibly changed through other association or by hand directly in database. To receive actual products do c.products.reload
, to count cached products(without sql triggering) do c.products.size
See detailed explanation here https://stackoverflow.com/a/18997294
Ruby / Rails - .each Iterator is printing entire array at the end of the loop
# Change this line with an =:
<%= @categories.each do |c| %>
# ...to this:
<% @categories.each do |c| %>
You only want the side effects on the block of the #each
method, you don't want interpolation of the returned value.
Each loop outputs an unwanted dump of the records after the loop
What's happening here is that you're using <%= %>
, which is the ERB tag that evaluates its content as Ruby and then prints it to the page. But you don't want to print out the value of @post.comments.each do |comment|
; you just want to evaluate it. That's what the <% %>
tag (no =
) is for.
The reason you're seeing the text you are is that <%= %>
implicitly calls to_s
on its content.
This answer has a full list of ERB tag types: https://stackoverflow.com/a/7996827/882025
Why is the last array in my loop the only array to display in my view?
@event_class_array.each do |x|
@event_class = x
end
this code means that for each array, @event_class is rewrited with the x. So only the last one is stored there.
If you want to insert all arrays you should do
@event_class = []
@event_class_array.each do |x|
@event_class << x
end
Rails each_slice displaying array at bottom of page
The problematic part is = group.each do |post|
since each
returns the array itself as well in addition to running the loop. And since you have an =
sign before it so the =
in the view
would also render the returned output of the group.each
statement. You may want to use -
as - group.each do |post|
as it will render the output of group.each
.
how to convert array output to normal string in ruby on rails application
Did you look at pluck
? This if very useful for if you want just one record from the db (in your case 'name'). You could use that to do this:
a = article.tags.pluck(:name)
To then output your article names separated by spaces do this:
a.join(" ")
For completeness sake, you can chain these methods (like you said in the comment below) like this:
article.tags.pluck(:name).join(" ")
Related Topics
How to Track the Execution Process of Ruby Program
Decrypting Salted Aes File Generated on Command Line with Ruby
Parse Email Addresses for "From" and "To" Fields in Ruby
Ruby-Debug19 on Ruby-1.9.3-Preview1
Functional Code Examples in Ruby
Deleting a Modified Object from a Set in a No-Op
Escape Single Quote in Xpath with Nokogiri
In Ruby Why Does Nil.Id Return 4
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
Ruby Mocha: Is There an Equivalent to Rspec-Mocks' #And_Call_Original
How Does Require Rubygems Help Find Rubygem Files
301 Moved Permanently After S3 Uploading
What Does &: Mean in Ruby, Is It a Block Mixed with a Symbol
Difference Between Downcase and Downcase! in Ruby
How to Set the Socket Timeout in Ruby
Query Records Through Its Belongs_To Relation in Rails
Lisp and Erlang Atoms, Ruby and Scheme Symbols. How Useful Are They