rails 3 routing not working as i would like - ruby-on-rails

How would i go about routing the default page in my rails application to :
http://localhost:3000/pages/1
at the moment in my routes file i have:-
root :to => 'pages#show'

Just add the id param like:
root :to => 'pages#show', :id => 1

Related

Spina CMS routes set default landing page

I have Rails app that uses Spina CMS,
I want the landing page \ to be one of the pages in admin.
for ex. localhost:3000\home is my localhost:3000
currently getting 404 setting root :to => 'pages#home'
Seems root :to ... is getting overriden by Spina routes
routes.rb
Rails.application.routes.draw do
match '(*any)', to: redirect(subdomain: ''), via: :all, constraints: {subdomain: 'www'}
mount Spina::Engine => '/'
root :to => 'pages#home' # => not working...
get '/*id' => 'pages#show', as: "page", controller: 'pages', constraints: lambda { |request|
!(request.env['PATH_INFO'].starts_with?('/rails/') || request.env['PATH_INFO'].starts_with?("/#{Spina.config.backend_path}") || request.env['PATH_INFO'].starts_with?('/attachments/'))
}
end
In the route page replace root :to => 'pages#home' to root 'pages#home'
and add
resources :pages
Let me know if this works for you :)

Prefixed route path gives error in Rails 4

After upgrading to Rails 4 a route with a prefixed name and slash is throwing an error.
actionpack-4.0.1.rc1/lib/action_dispatch/routing/mapper.rb:239:in `default_controller_and_action':
'MyEngine/dashboard'
is not a supported controller name. This can lead to potential routing problems.
In routes.rb I have
Rails.application.routes.draw do
mount MyEngine::Engine => "/foo", :as => 'my_engine'
match 'dashboard' => 'MyEngine/dashboard', via: :get
And in the mounted engine MyEngine:
MyEngine::Engine.routes.draw do
match 'dashboard' => 'dashboard#index', via: :get
This works well in Rails 3.2, but in Rails 4 the slash in 'MyEngine/dashboard' throws the error.
Using an engine, you can directly create routes to the engine's controllers and actions in your routes file like this:
Rails.application.routes.draw do
mount MyEngine::Engine => "/foo", :as => 'my_engine'
get 'dashboard' => 'dashboard#index'
end
I think it's not possible to set a route in the host application to a controller in a mounted engine (mounted on "/foo") at the top level, e.g. /foo/dashboard calls the mounted engine's 'dashboard#index' action, and I want /dashboard to do the same.
I'm adding a controller of the same name in the host application and doing a redirect to the mounted engine controller action.
Simply, change this line
match 'dashboard' => 'dashboard#index', via: :get
as
get 'dashboard' => 'dashboard#index'

Controller to match root routes

I want to have a controller active at the root of my application, so:
www.example.com/1 would return
the post with an ID of 1 in the foo controller.
How can I do this?
get '/:id', :controller => 'foo', :action => 'show'
seems to work. Is this the best way to do it?
in your config/routes.rb file, put:
root "TheControllerName.TheMethodName"
Older Rails versions may need:
root :to => "TheControllerName.TheMethodName"
For rails 3 add this to your routes.rb.
get '/:id' => 'foo#show', :as => 'show'
http://guides.rubyonrails.org/routing.html

Dealing with multiple root paths and scopes in Rails

We have the following routes setup:
MyApp::Application.routes.draw do
scope "/:locale" do
...other routes
root :to => 'home#index'
end
root :to => 'application#detect_language'
end
Which gives us this:
root /:locale(.:format) home#index
root / application#detect_language
which is fine.
However, when we want to generate a route with the locale we hitting trouble:
root_path generates / which is correct.
root_path(:locale => :en) generates /?locale=en which is undesirable - we want /en
So, question is, is this possible and how so?
root method is used by default to define the top level / route.
So, you are defining the same route twice, causing the second definition to override the first!
Here is the definition of root method:
def root(options = {})
options = { :to => options } if options.is_a?(String)
match '/', { :as => :root, :via => :get }.merge(options)
end
It is clear that it uses :root as the named route.
If you want to use the root method just override the needed params.
E.g.
scope "/:locale" do
...other routes
root :to => 'home#index', :as => :root_with_locale
end
root :to => 'application#detect_language'
and call this as:
root_with_locale_path(:locale => :en)
So, this is not a bug!

Rails 3 - Conditional Routing?

I have a routing question about rails 3, and setting up a conditional :root path.
Right now, my route.rb has the following:
root :to => "topics#index"
This is great and dandy, but only if a user is on their specific subdomain (basecamp style) on my site. If they go to www.myapp.com or myapp.com, this should not be the same :root. I was wonder if this was at all possible to setup, something that would be like...
if default_subdomain(www, "")
root :to => "promos#index"
else
root :to => "topics#index
end
I know this wouldn't be allowed in the routes.rb, but something that would do the same logical thing. Does anyone have any experience with this, or any documentation/blog that I could read over to try to set something like this up.
Thanks
Per chuck's help below(thanks a ton), this ended up being my working code:
constraints(:subdomain => "www") do
root :to => "promos#index"
end
root :to => "topics#index"
You can use the :requirements tag to accomplish this.
root :to => "promos#index", :requirements => { :subdomain => "www" }
root :to => "topics#index"
I think this will work. I've never encountered it going by sub-domain/lack of a subdomain.
Edit: After doing some reading, Rails 3 uses :constraints instead.

Resources