0

Very confused about what is probably a simple issue. Trying to show just a column of my table by iterating through it but somehow before this happens my entire table is displayed.

Output:

All My Stories

[#Story id: 1, thought: nil, created_at: "2016-02-11 03:20:07", updated_at: "2016-02-11 03:20:07", #Story id: 2, thought: "hello my name is Patrick", created_at: "2016-02-11 03:22:04", updated_at: "2016-02-11 03:22:04", #Story id: 3, thought: "Dennis is cool", created_at: "2016-02-11 03:22:37", updated_at: "2016-02-11 03:22:37"]

Thoughts

hello my name is Patrick

Dennis is cool

Controller code:

class StoriesController < ApplicationController

def index
  @stories = Story.all
end

def show
  @story = Story.find(params[:id])
end

def new
  @story = Story.new
end

def create
  @story = Story.new(story_params)

  if @story.save
    redirect_to @story
  else
    render 'new'
  end
end

private
  def story_params
    params.require(:story).permit(:thought)
  end

end

View Code:

<h1>All My Stories</h1>

<table>
<tr>
  <th>Thoughts</th>
</tr>

  <%= @stories.each do |s| %>
    <tr>
      <td><%= s.thought %>
    </tr>
  <% end %>
</table>

Created my model using rails generate model Story thought:string

2 Answers2

1

That's b/c you've a = in <%=, which will print @stories.

Try this.

<% @stories.each do |s| %>
  <tr>
    <td><%= s.thought %>
  </tr>
<% end %>
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
-1

Remove the = before your @stories on your view. It should be like this:

<% @stories.each do |s| %>
  # your code here
<% end %>
Lymuel
  • 574
  • 3
  • 10
  • YES!!! Thank you. Holy moly guacamole. This was driving me nuts and even doing tutorials I was always wondering what the syntax was. I know that both % and %= are used for embedded ruby but I finally found the full answer [here](https://stackoverflow.com/questions/3952403/without-equal-in-ruby-erb-means) have my upvotes but I'm not sure they do anything from me yet – Patrick Kinsella Feb 11 '16 at 19:41
  • Why did you post the same answer, almost two hours after me? – Linus Oleander Feb 11 '16 at 19:47