Question

Question is how to specify a route just for image tags? I am customizing devise sessions/new.html.erb. My app is looking in "/users/assets/images/carousel/2.jpg" instead of "/assets/images/carousel/2.jpg". How do I change that?

I am here in my app: http://localhost:3000/users/sign_in

I have this in my view:

  <!-- 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>

This is my console log:

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"):

These are my routes:

 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
Was it helpful?

Solution 2

You are getting a routing error because Rails is looking for the image sources in the wrong directory.

Try this:

<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>

I'd consider doing this with a loop:

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

You could set the index of the active item to some variable, and use that to set the active:

<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 %>

OTHER TIPS

By default Rails will look for assets from 'app/assets', 'lib/assets' and 'vendor/assets' folders and if you really want to customize it,

please add the below line config/application.rb:

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

Check in you rails console what all assets have been added to your asset path and those will only be available for your application.

Rails.application.config.assets.paths

Hope this helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top