alcol92
alcol92

Reputation: 165

Ruby on Rails: Displaying model attribute on view

So I have a user model and a store model created. I am having a problem displaying the name of a store that has already been created in my database. It's a very stupid problem, with a very easy solution, but I just can't figure out what I'm doing wrong. So here is my user controller where I find the store I want to display:

def show
   @user = User.find(params[:id])
   @store = Store.find(1)
end

And here is the view:

<% provide(:title, @user.username) %>
 <div class="row">
  <aside class="col-md-4">
   <section class="user_info">
     <h1>
       <%= gravatar_for @user %>
       <%= @user.username %>
     </h1>
   </section>
 </aside>
</div>

<li><% @store.name %></li>

So the name of the user shows up, no issues there, but for some reason, the name of the store is not showing up. I know as a fact that there is a store with an id: 1 in the database. Any help here would be greatly appreciated!

Upvotes: 0

Views: 1881

Answers (3)

Sharvy Ahmed
Sharvy Ahmed

Reputation: 7405

Lets see which ERB tag does what:

<%= %> # Prints the attribute value.
Welcome to <%= @store.name %>!


<% %> # Executes the code.
<ul>
  <% @stores.each do |store| %>
  <li><%= store.name %></li>
  <% end %>
</ul>


<%# %> # Comments out the code.
<%# This is just a comment... %>

Rails extends ERB, so you can use this shortcut to suppress new line:

<% -%> # Avoids line break after expression.
<ul>
  <% @stores.each do |store| -%>
  <li><%= store.name %></li>
<% end %>
</ul>

For further study, see here.

Upvotes: 0

jon snow
jon snow

Reputation: 3072

Make it like this,

<li><%= @store.name %></li>

Upvotes: 0

infused
infused

Reputation: 24337

To output content with ERB, you need to add an = to the ERB block:

<%= @store.name %>

Upvotes: 5

Related Questions