-1

When I go to an index view for my Rails program, it is suppose to iterate over all the object's properties and display each one in turn. My program is doing this perfectly as expected.

It is ALSO then displaying an array of each instance below the normal list. I am not sure why. Any thoughts?

In the controller:


def index
  @user = User.find(params[:user_id])
  @characters = @user.characters.all
end

In the View

<%= @characters.each do |char| %>
  <%= char.name %>
<% end %>

What is displayed on screen from the above:

Joe Jane [#<Character id: 1, allies: "none", enemies: "none", description: "something", user_id: 6, race_id: 1, created_at: "2019-08-11 08:37:08", updated_at: "2019-08-11 08:37:08", name: "Joe">, #<Character id: 2, allies: "none", enemies: "none", description: "something", user_id: 6, race_id: 1, created_at: "2019-08-11 08:39:49", updated_at: "2019-08-11 08:39:49", name: "Jane">]

I expect the index page for a user's characters to only display

JoeJane

Currently it actually displays:

Joe Jane [#Character id: 1, allies: "none", enemies: "none", description: "something", user_id: 6, race_id: 1, created_at: "2019-08-11 08:37:08", updated_at: "2019-08-11 08:37:08", name: "Joe">, #Character id: 2, allies: "none", enemies: "none", description: "something", user_id: 6, race_id: 1, created_at: "2019-08-11 08:39:49", updated_at: "2019-08-11 08:39:49", name: "Jane">]

The program seems to be with me running the loop at all. I have taken out the char.name part and just run an empty block and get the same result as above minus the Joe Jane bit. I have reset my server, dropped and reloaded by database, and compared it to similar code I have on other projects that do not produce these odd results. I am not sure what is causing this oddity.

Theo
  • 57,719
  • 8
  • 24
  • 41
  • 1
    I am not very active in the [tag:ruby-on-rails] tag because I only do [tag:ruby], but I know for a fact that this exact same question has been asked and answered dozens of times already on [so]. So, someone who is more knowledgeable than me about Rails should choose an appropriate dup target and close the question. – Jörg W Mittag Aug 11 '19 at 11:07
  • I didn't see it asked anywhere else. Please feel free to point me to where you think it has. As it turns out it was a mere typo with me including an = sign where it did not go. – ForeignOrchid Aug 11 '19 at 13:24
  • I'm not a [tag:ruby-on-rails] user, so I'm not really familiar with that tag. A couple of examples I found: https://stackoverflow.com/q/4065356 https://stackoverflow.com/q/4665279 https://stackoverflow.com/q/6026029 https://stackoverflow.com/q/7505146 https://stackoverflow.com/q/9934251 https://stackoverflow.com/q/11369751 https://stackoverflow.com/q/11581570 https://stackoverflow.com/q/13219475 https://stackoverflow.com/q/16651061 https://stackoverflow.com/q/17955135 https://stackoverflow.com/q/19911474 https://stackoverflow.com/q/20274464 https://stackoverflow.com/q/23167161 – Jörg W Mittag Aug 11 '19 at 16:18
  • https://stackoverflow.com/q/25586001 https://stackoverflow.com/q/28822568 https://stackoverflow.com/q/30633707 https://stackoverflow.com/q/31843042 https://stackoverflow.com/q/33086509 https://stackoverflow.com/q/35397683 https://stackoverflow.com/q/35406606 https://stackoverflow.com/q/35458373 https://stackoverflow.com/q/36095811 https://stackoverflow.com/q/38174153 https://stackoverflow.com/q/40600428 https://stackoverflow.com/q/40854688 https://stackoverflow.com/q/41706300 https://stackoverflow.com/q/42037394 https://stackoverflow.com/q/44171670 https://stackoverflow.com/q/46834174 – Jörg W Mittag Aug 11 '19 at 16:20
  • https://stackoverflow.com/q/47047514 https://stackoverflow.com/q/47615238 https://stackoverflow.com/q/49131246 https://stackoverflow.com/q/54614119 https://stackoverflow.com/q/54641636 – Jörg W Mittag Aug 11 '19 at 16:20

1 Answers1

0

Change

<%= @characters.each do |char| %>

to

<% @characters.each do |char| %>
  • <% => execute code
  • <%= => execute code and show in page
Ursus
  • 29,643
  • 3
  • 33
  • 50