Routing to static html page in /public - ruby-on-rails

How can I route /foo to display /public/foo.html in Rails?

You can do this:
Add this, into your routes.rb file.
match '/foo', :to => redirect('/foo.html')
Update
In Rails 4, it should use "get", not "match":
get '/foo', :to => redirect('/foo.html')
thanks Grant Birchmeier

This can be done without triggering a redirect. Follow the steps further down to be able to route static files in config/routes.rb as shown in this example:
# This route will serve public/index.html at the /login URL
# path, and have a URL helper named `login_path`:
get "/login", to: static("index.html")
# This route will serve public/register.html at the /register
# URL path, and have URL helper named `new_user_registration_path`:
get "/register", to: static("register.html"), as: :new_user_registration
Install the rails-static-router gem: https://github.com/mufid/rails-static-router#installation
Restart app (first bin/spring stop to be sure app is completely reloaded).
Start using the static(path) method in your config/routes.rb.

E.g in Rails 4 add the following route:
get '/example', :to => redirect('example.html')
Also you need to enable static files from the 'public' directory in your configuration:
config.serve_static_files = true
OR
config.serve_static_assets = true
Also you might need to provide your public directory as root in NGINX configuration.

Related

Rails Engine not routing in webpage

I am trying to mount a rails engine piggybak_paypal.
Inside my application's config/routes.rb file I add
mount PiggybakPaypal::Engine => '/paypal', :as => 'piggybak_paypal'
and the engines route is like this
PiggybakPaypal::Engine.routes.draw do
get "/express" => "paypal#express", :as => :paypal_express
get "/process" => "paypal#process_express", :as => :paypal_process
end
When I try rake routes the route of the engine is showing correctly
Routes for PiggybakPaypal::Engine:
paypal_express GET /express(.:format) piggybak_paypal/paypal#express
paypal_process GET /process(.:format) piggybak_paypal/paypal#process_express
but when I open my website and use /express the server cannot find the route
ActionController::RoutingError (No route matches [GET] "/express"):
I've look around but I can't find a solution to it.
okay I just figure out why the routing is not working and I can't believe I spend hours on this and didn't found out why.
All I did is change the route from
mount PiggybakPaypal::Engine => '/paypal', :as => 'piggybak_paypal'
to
mount PiggybakPaypal::Engine => '/', :as => 'piggybak_paypal'
then /express works.

How do I selectively enable SSL in Rails 4 for certain paths?

How do I turn SSL HTTPS off for a given path? I saw Enable SSL for certain resource actions but that was to enable HTTPS for a single path. My config has
config.force_ssl = true
However when I show a page that has an iframe and embeds an external source
<iframe src='http://www.
Then it doesn't even appear (Chrome) because it is mixed content. So I want to disable SSL for that single route that has an iframe. (I really want to turn it on & off depending on the content.)
I tried adding this to my routes.rb
get 'frame', :constraints => { :protocol => 'http' }
but that gave an error http://127.0.0.1:3000/posts/136/frame
No route matches [GET] "/posts/136/frame"
even though the route shows up
frame_post GET /posts/:id/frame(.:format) posts#frame {:protocol=>"http"}
I also saw Force SSL for specific routes in Rails 3.1 and tried
gem 'rack-ssl-enforcer' # Gemfile
config.middleware.use Rack::SslEnforcer, :except => [ /\/frame$/ ], :strict => true # application.rb
but it still always redirects to https. I also tried
force_ssl :except => :frame # posts_controller.rb
But that didn't work either.
I'm using Rails 4.1.
Rack ssl enforcer is a great solution for this - your regular expression is wrong. In your form, the regex would only exclude a path that is entirely '/frame', and would not exclude a path that had anything before /frame
If you want to exclude any path that ends with /frame, The correct regular expression would be:
\.*\/frame$/
Resulting in a config:
config.middleware.use Rack::SslEnforcer, :except => [ \.*\/frame$/ ], :strict => true

Refinery Image Uploading giving no route match (How do you change default image route in refinery?)

When uploading an image to the Refinery CMS I get a broken link.
No route matches [GET] "/system/images/W1siZiIsIjIwMTMvMDUvMDgvMjFfMjBfMjFfMzlfc3RlYWtfZmluYWwuanBnIl1d/steak-final.jpg"
Any idea why could this be happening or is there a fix?
UPDATE: The problem is that I am mounting Refinery on "/cms" instead of the root path.
But I still don't know how to change the picture route. If I add:
/cms
in front of the image path it works.
ROUTES
root :to => 'static#home'
get "vault", :to => "restricted_content#vault"
Refinery::Core::Engine.routes.prepend do
get '/courses/:id/classroom', :to => 'courses/courses#classroom'
end
mount Refinery::Core::Engine, :at => '/cms'
devise_for :users
RefineryCMS needs to be mounted at root to work properly. Why is it important to mount it at cms?
Read through this issue # github. I've provided a monkeypatch for this issue.

Basic Rails3 routes question?

My routes.rb has the following 2 lines:
match "/", :to => "main#index"
match "main/index", :to => "main#index"
When I type localhost:3000/main/index in a browser I see the proper page (views/main/index.html.erb).
However, if I type just localhost:3000 I see public/index.html (I expect to see views/main/index.html.erb as well).
What am I missing ?
Remove public/index.html:
rm public/index.html
This is happening because static files (those in public/) are served in priority to the routes.

Rails 3 & Sinatra integration issue

I am trying to set up a sinatra app inside my Rails 3 (v3.0.1) application, but having no success. Sinatra gem (v1.1.0) is setup using bundle install.
Here's what i have.
customer_app.rb class in lib directory -
class CustomerApp < Sinatra::Base
get "/test" do
"Hello World"
end
end
my routes.rb file contains -
CustomerService::Application.routes.draw do
root :to => CustomerApp
end
The URL i am trying is - http://localhost:3000/test
I get this error (on the browser) - Routing Error. No route matches "/test"
and this error in the log - ActionController::RoutingError (No route matches "/test"):
Is there anything I am missing??
Also I just noticed, even a simple rack route is not working -
root :to => proc { |env| [200, {}, ["Welcome!"]]}
The root keyword by default maps only the path /.
So you are trying to say, forward any request for / to CustomerApp which can handle requests for /test.
You should change the match filter.
CustomerService::Application.routes.draw do
match "/test" :to => CustomerApp
end

Resources