ruby-on-railspublic-html

rails: use ruby in public/index


I'd like to use ruby in my public index.html (e.g. for the stylesheet or a form). But I can't figure out how to do this :S

If I rename it to index.html.erb or index.erb it's not recognized by rails. instead my root from the routes is used (which is part of the app and needs the user to be logged in).


Solution

  • Anything in the public directory is rendered as a static asset, so no Ruby code in there.

    Instead, you should remove the index.html file in that directory, and replace it with something else within your app. For example, you can do something like this:

    # app/controllers/site_controller.rb
    class SiteController < ApplicationController
      def index
        # ...
      end
    end
    
    # config/routes.rb
    root to: 'site#index'
    
    # app/views/site/index.html.erb
    <!-- Your HTML and Ruby here -->
    

    This will give you the behavior you want.