0

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!

3 Answers 3

5

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

<%= @store.name %>
Sign up to request clarification or add additional context in comments.

Comments

0

Make it like this,

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.