Search code examples
ruby-on-railsrubyassets

Why the path to assets is added after path segment ? Rails


When I added a path segment to my Ruby on Rails app /pricing the path to assets started to be added after it like this which causes 404:

GET http://localhost:3000/pricing/assets/bootstrap.min.css 

This is the route:

get 'pricing/:level', :to => 'welcome2#pricing', as: "package_signup"

This is the controller:

class Welcome2Controller < ApplicationController
  def pricing
    @package_signup = params[:level] 
  end
end

This is the link on the index page linking to pricing/bronze:

<%= link_to 'package_signup bronze', package_signup_path('bronze') %><button class="btn btn-success">Get Started</button>

How can I keep the path to assets the same ? like this:

GET http://localhost:3000/assets/bootstrap.min.css 

This is where bootstrap is included:

<link href='assets/bootstrap.min.css' rel="stylesheet">

Solution

  • Add a / (forward slash) before assets. It ensures that assets will be picked from root URL not from the current URL.

    Like this:

    <link href='/assets/bootstrap.min.css' rel="stylesheet">