Michael Liu
Michael Liu

Reputation: 1131

Rails 4 - Image doesn't show up on model's new page

I have a sidebar file in my /views/layouts/ folder that has a header image accessible at /assets/images/ However, when I go to localhost:3000/books/new the sidebar header image is unable to be displayed. Looking in the terminal, it's looking for the header image in /books/assets/images/

Is there a way to fix this error quickly without having to copy my images into new folders?

__sidebar.html.haml

%img#menu-image{src: "assets/books.png"}
.pure-menu.pure-menu-open#menu-bottom.button-color
  %ul
    %li
      =link_to 'Home', root_path
    %li
      =link_to 'About', about_path
    %li
      =link_to 'Find Textbooks', search_path
    %li
      =link_to 'List your Textbooks', new_book_path

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Bookweb</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
  <%= favicon_link_tag 'favicon.ico' %>
</head>
<body>

<div class="pure-g-r" id="main-pad">
  <div class="pure-u" id="menu">
    <%= render 'layouts/sidebar' %>
  </div>
  <div class="pure-u-1" id="main">
    <%= yield %>
  </div>
</div>

</body>
</html>

Upvotes: 0

Views: 289

Answers (2)

sevenseacat
sevenseacat

Reputation: 25049

Rails comes with a whole lot of view helpers to help you generate HTML easily. one of these is image_tag.

Use it like so: = image_tag 'books.png', :id => 'menu-image'

Upvotes: 1

Bigxiang
Bigxiang

Reputation: 6712

In Rails, if you need reference an asset on pages, you should use asset helpers. In this case, you can change the line:

%img#menu-image{src: "assets/books.png"}

to

%img#menu-image{src: "#{image_path('books.png')}"}

Upvotes: 1

Related Questions