Rails 4.0 - 资产路径是否根据路线变化?

时间:2014-08-13 02:29:15

标签: ruby ruby-on-rails-4 routes

在我的application.rb文件中,我有以下内容:

config.assets.paths << Rails.root.join('app', 'assets')

在我的routes.rb文件中,我有这个:

root 'admin#index'

完美无缺。我的所有资产都已正确加载,并在html文件中引用它们:

assets/stylesheets/...

现在,如果我尝试这样做,找不到资产,因为它正在寻找错误的网址:

get 'admin/sign_in' => 'admin#sign_in'

当我导航到localhost:3000/admin/sign_in时,我收到了一堆404错误。资产正在尝试加载:

localhost:3000/admin/assets/....

当我导航到自定义路线时,为什么资产会在/admin/assets处加载,但导航到他们在/assets处加载的根路线?

编辑:

我的application.html.erb如下:

<!DOCTYPE html>
<html>
<head>
  <title>Website</title>
  <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

<%= yield %>

</body>
</html>

编辑2:

我正在使用一个主题,我的html文件现在看起来与此类似:

<link href='assets/images/meta_icons/favicon.ico' rel='shortcut icon' type='image/x-icon'>
    <link href='assets/images/meta_icons/apple-touch-icon.png' rel='apple-touch-icon-precomposed'>
    <link href='assets/images/meta_icons/apple-touch-icon-57x57.png' rel='apple-touch-icon-precomposed' sizes='57x57'>
    <link href='assets/images/meta_icons/apple-touch-icon-72x72.png' rel='apple-touch-icon-precomposed' sizes='72x72'>
    <link href='assets/images/meta_icons/apple-touch-icon-114x114.png' rel='apple-touch-icon-precomposed' sizes='114x114'>
    <link href='assets/images/meta_icons/apple-touch-icon-144x144.png' rel='apple-touch-icon-precomposed' sizes='144x144'>
    <!-- / START - page related stylesheets [optional] -->

    <!-- / END - page related stylesheets [optional] -->
    <!-- / bootstrap [required] -->
    <link href="assets/stylesheets/bootstrap/bootstrap.css" media="all" rel="stylesheet" type="text/css" />
    <!-- / theme file [required] -->
    <link href="assets/stylesheets/light-theme.css" media="all" id="color-settings-body-color" rel="stylesheet" type="text/css" />
    <!-- / coloring file [optional] (if you are going to use custom contrast color) -->
    <link href="assets/stylesheets/theme-colors.css" media="all" rel="stylesheet" type="text/css" />
    <!-- / demo file [not required!] -->
    <link href="assets/stylesheets/demo.css" media="all" rel="stylesheet" type="text/css" />
    <!--[if lt IE 9]>
      <script src="assets/javascripts/ie/html5shiv.js"
              type="text/javascript"></script>
      <script src="assets/javascripts/ie/respond.min.js"
              type="text/javascript"></script>
      <![endif]-->





<!-- / jquery [required] -->
    <script src="assets/javascripts/jquery/jquery.min.js" type="text/javascript"></script>
    <!-- / jquery mobile (for touch events) -->
    <script src="assets/javascripts/jquery/jquery.mobile.custom.min.js" type="text/javascript"></script>
    <!-- / jquery migrate (for compatibility with new jquery) [required] -->
    <script src="assets/javascripts/jquery/jquery-migrate.min.js" type="text/javascript"></script>
    <!-- / jquery ui -->
    <script src="assets/javascripts/jquery/jquery-ui.min.js" type="text/javascript"></script>
    <!-- / jQuery UI Touch Punch -->
    <script src="assets/javascripts/plugins/jquery_ui_touch_punch/jquery.ui.touch-punch.min.js" type="text/javascript"></script>
    <!-- / bootstrap [required] -->
    <script src="assets/javascripts/bootstrap/bootstrap.js" type="text/javascript"></script>
    <!-- / modernizr -->
    <script src="assets/javascripts/plugins/modernizr/modernizr.min.js" type="text/javascript"></script>
    <!-- / retina -->
    <script src="assets/javascripts/plugins/retina/retina.js" type="text/javascript"></script>
    <!-- / theme file [required] -->
    <script src="assets/javascripts/theme.js" type="text/javascript"></script>
    <!-- / demo file [not required!] -->
    <script src="assets/javascripts/demo.js" type="text/javascript"></script>
    <!-- / START - page related files and scripts [optional] -->
    <script src="assets/javascripts/plugins/validate/jquery.validate.min.js" type="text/javascript"></script>
    <script src="assets/javascripts/plugins/validate/additional-methods.js" type="text/javascript"></script>
    <!-- / END - page related files and scripts [optional] -->

2 个答案:

答案 0 :(得分:1)

尝试将文件从.html更改为.html.erb,并使用stylesheet_link_tag来要求资产:

<%= stylesheet_link_tag "light-theme" %>

这应该为你解决问题。

答案 1 :(得分:0)

我相信Rails会像您所写的那样在/assets/image-name.jpg/assets/stylesheet-name.css/assets/application.js以及no9t assets/images/meta_icons/apple-touch-icon.png投放资产。

GET&#39; admin / sign_in&#39;,它在views/admin/

中寻找sign_in.html.erb模板