Basic Rails3 routes question? - ruby-on-rails

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.

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.

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.

rspec chokes on named path in rails 3

Re-writing a photography portfolio site, and urls have to follow the format:
[site].com/portfolio/[category]/[image]
to match current implementation.
[site].com and [site].com/portfolio should resolve to the root. my routes file is below:
root :to => 'portfolio#index'
match '/portfolio', :to => 'portfolio#index'
match '/portfolio/*category/*photo', :to => 'portfolio#photo'
match '/portfolio/*category', :to => 'portfolio#photo'
I added a link in my header to:
%li= link_to "Portfolio", root_path
and this works fine, but when I add:
%li= link_to "Editorial", portfolio_photo_path
my pages resolve fine in-browser, but rspec chokes out on me; even the simplest http success tests that would previously run fine now return:
1) PortfolioController GET 'index' returns http success
Failure/Error: get 'index'
ActionView::Template::Error:
undefined local variable or method `portfolio_photo_path' for #<#<Class:0x572d210>:0x5733d68>
# ./app/views/layouts/_header.html.haml:7:in `_app_views_layouts__header_html_haml__420220390_28357236'
# ./app/views/layouts/application.html.haml:10:in `_app_views_layouts_application_html_haml__156489427_30065868'
# ./spec/controllers/portfolio_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
(when I rake routes, i get the following)
$ rake routes
root / portfolio#index
portfolio /portfolio(.:format) portfolio#index
/portfolio/*category/*photo(.:format) portfolio#photo
/portfolio/*category(.:format) portfolio#photo
You should add ":as => :portfolio" statement:
match '/portfolio', :to => 'portfolio#index', :as => :portfolio
Answered in my comment above; I'd defined variable-globbed routes, and brain-fartedly didn't realize that i'd have to construct the links directly and dynamically.

Routing to static html page in /public

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.

Rails Routing help

I have the following in my routes.rb:
match "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions', :via => :get
match "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions', :via => :post
Getting /profile/permissions works find, yet, when I post to /profile/permissions, I get:
Started POST "/profile/permissions" for 127.0.0.1 at 2011-03-29 12:12:09 -0400
ActionController::RoutingError (No route matches "/profile/permissions"):
Any ideas?
Can you put your routes.rb in a gist? Sometimes there's a conflict, and extra eyes are needed to find it. Also, for the sake of fidgeting, you might try out the new shorthand:
get "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions'
post "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions'
Unto themselves, both your routing text and this example successfully trigger their actions on the specified controller.
Start a test app, with only a routing file, run its server, and try to post to those URLs. If you want to use the Rails console, check out this.
> require "uri"
> require "net/http"
> url = app.update_permissions_url
> url = "http://localhost/profile/permissions" (if your domain is bollocks)
> Net::HTTP.post_form(URI.parse(url),{})
Easy to minimalize your environment for testing, now.

Resources