Rails Bootstrap资产/图像rake路由路由错误?

时间:2014-03-31 12:13:06

标签: ruby-on-rails routes rake

问题是如何为图像标签指定路线?我正在定制设计会话/ new.html.erb。我的应用程序正在查看" /users/assets/images/carousel/2.jpg"而不是" /assets/images/carousel/2.jpg"。我该如何改变?

我在我的应用中:http://localhost:3000/users/sign_in

我认为这是:

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
    <div class="item active">
      <img src="assets/images/carousel/1.jpg" alt="1"/>
    </div>
  <div class="item">
      <img src="assets/images/carousel/2.jpg" alt="2"/> 
  </div>
 <div class="item">
      <img src="assets/images/carousel/3.jpg" alt="3"/> 
  </div>
  </div>

这是我的控制台日志:

Started GET "/users/assets/images/carousel/2.jpg" for 127.0.0.1 at 2014-03-31 13:10:12 +0100

ActionController::RoutingError (No route matches [GET] "/users/assets/images/carousel/2.jpg"):

这些是我的路线:

 Prefix Verb   URI Pattern                    Controller#Action
                   bboys GET    /bboys(.:format)               bboys#index
                         POST   /bboys(.:format)               bboys#create
                new_bboy GET    /bboys/new(.:format)           bboys#new
               edit_bboy GET    /bboys/:id/edit(.:format)      bboys#edit
                    bboy GET    /bboys/:id(.:format)           bboys#show
                         PATCH  /bboys/:id(.:format)           bboys#update
                         PUT    /bboys/:id(.:format)           bboys#update
                         DELETE /bboys/:id(.:format)           bboys#destroy
           ranking_index GET    /ranking(.:format)             ranking#index
                         POST   /ranking(.:format)             ranking#create
             new_ranking GET    /ranking/new(.:format)         ranking#new
            edit_ranking GET    /ranking/:id/edit(.:format)    ranking#edit
                 ranking GET    /ranking/:id(.:format)         ranking#show
                         PATCH  /ranking/:id(.:format)         ranking#update
                         PUT    /ranking/:id(.:format)         ranking#update
                         DELETE /ranking/:id(.:format)         ranking#destroy
                    rate POST   /rate(.:format)                rater#create
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        registrations#cancel
       user_registration POST   /users(.:format)               registrations#create
   new_user_registration GET    /users/sign_up(.:format)       registrations#new
  edit_user_registration GET    /users/edit(.:format)          registrations#edit
                         PATCH  /users(.:format)               registrations#update
                         PUT    /users(.:format)               registrations#update
                         DELETE /users(.:format)               registrations#destroy
              home_index GET    /home/index(.:format)          home#index
                    root GET    /                              bboys#index

2 个答案:

答案 0 :(得分:2)

默认情况下,Rails会查找'app/assets''lib/assets''vendor/assets'个文件夹中的资源,如果您确实要自定义它,

请添加以下行config/application.rb

# config/application.rb
 config.assets.paths << Rails.root.join("users", "assets", "images")

检查rails console所有资产已添加到资产路径中的内容,这些资产仅适用于您的应用。

Rails.application.config.assets.paths

希望这会有所帮助:)

答案 1 :(得分:1)

您收到路由错误,因为Rails正在错误的目录中查找图像源。

试试这个:

<div class="carousel-inner">
  <div class="item active">
    <%= image_tag "carousel/1", :alt => "1" %>
  </div>
  <div class="item">
    <%= image_tag "carousel/2", :alt => "2" %>
  </div>
  <div class="item">
    <%= image_tag "carousel/3", :alt => "3" %>
  </div>
</div>

我考虑用循环来做这件事:

<div class="carousel-inner">
  <% (1..3).each do |i| %>
    <div class="item">
      <%= image_tag "carousel/#{i.to_s}", :alt => i.to_s %>
    </div>
  <% end %>

您可以将活动项目的索引设置为某个变量,并使用它来设置活动项目:

<div class="carousel-inner">
  <% (1..3).each do |i| %>
    <div class="item <%= 'active' if i == active_index %>">
      <%= image_tag "carousel/#{i.to_s}", :alt => i.to_s %>
    </div>
  <% end %>