繁体   English   中英

Rails Bootstrap资产/映像耙路径路由错误?

[英]Rails Bootstrap assets/images rake routes routing error?

问题是如何只为图像标签指定路线? 我正在自定义devise session / 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

默认情况下,Rails将从'app/assets''lib/assets''vendor/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

希望这可以帮助 :)

您收到路由错误,因为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 %>

暂无
暂无

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

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