简体   繁体   English

Rails 4 - 图像未显示在模型的新页面上

[英]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. 我的/ views / layouts /文件夹中有一个侧边栏文件,其头文件图像可在/ assets / images /访问。但是,当我转到localhost:3000/books/new ,无法显示侧边栏标题图像。 Looking in the terminal, it's looking for the header image in /books/assets/images/ 在终端中寻找/ books / assets / images /中的标题图片

Is there a way to fix this error quickly without having to copy my images into new folders? 有没有办法快速修复此错误,而无需将我的图像复制到新文件夹?

__sidebar.html.haml __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 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>

In Rails, if you need reference an asset on pages, you should use asset helpers. 在Rails中,如果您需要在页面上引用资产,则应使用资产助手。 In this case, you can change the line: 在这种情况下,您可以更改行:

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

to

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

Rails comes with a whole lot of view helpers to help you generate HTML easily. Rails附带了大量的视图助手,可以帮助您轻松生成HTML。 one of these is image_tag . 其中一个是image_tag

Use it like so: = image_tag 'books.png', :id => 'menu-image' 像这样使用它: = image_tag 'books.png', :id => 'menu-image'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM