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.
Related
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
I am creating a website were users can post jobs and users can login,register etc. I have created my jobs model and created my user model for the login/register part. When I try to load rails server I keep getting this error below and cannot figure out what I'm doing wrong or how to fix. I originally was trying to use devise and create the users model but was having issues so I deleted the files for it. I am wondering if I deleted something or if I am missing something in my routes.rb file. Can someone help or point me in the right direction? I will post my routes.rb file as well. Thanks for any guidance as I am still new to rails. The only thing I added to the routes.rb file was root :to => "sessions#login" and below that. Im sure other info in this was added when i created the models and controller.
/home/whitey7/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/mapper.rb:181:in `default_controller_and_action': missing :controller (ArgumentError)**
Routes.rb
Application.routes.draw do
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
resources :jobs
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "logout", :to => "sessions#logout"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
I think you still have a route in routes.rb which in invalid. Please recheck all the routes, corresponding controller and action. Please also share the full error trace, that way we can pin-point the issue.
Please check if the jobs controller is still there. Because this is the first root directive in your routes declarations (and still, it does not make sense to have more than one), Rails is checking if this root route is available. It seems that the jobs controller is missing and causing this error.
At first, check Rails routing documentation. I think you are getting this error because you are unable to define route file. The problem I figure out in your route file are :-
a. You have three different root in your route file.
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
b. You are defining same routes multiple times.
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
The solution might be as follows :-
a. Fix the root path at first. Which path you want to make root whether it is jobs index or home index or sessions login.
b. I think you are trying to define your routes as such
match "signup", :to => "users#new", via: :get
match "login", :to => "sessions#login", via: :get
match "home", :to => "sessions#home", via: :get
match "profile", :to => "sessions#profile", via: :get
match "setting", :to => "sessions#setting", via: :get
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!
I have an app, locally in development without a subfolder, and in production i have it deployed under /myappname/
So, locally I have http://myapp.dev and in production http://mydomain.com/myappname
which my root route does:
root :to => 'products#list'
which works great, even in production.
Now, i have a default match action:
match '/:controller(/:action(/:id))'
which breaks in production, so i started trying to build a restful route, but i need some help... I can't wrap my head around the routing. I think i have the proper start (with scope, below)
#PRODUCTION ROUTES
scope '/myappname' do
#WHAT WOULD GO HERE?
end
format would be /myappname/products/show/15
Hm. I would expect all the routes to work relative to the "home page", so why don't you just go with
resources :users
or any other route definition from the examples in config/routes.rb?
#PRODUCTION ROUTES
scope "/mothers" do
#ROOT
root :to => 'rings#list'
match '/rings/:id' => "rings#show", :as => :ring
end
#DEVELOPMENT ROUTES
root :to => 'rings#list'
match '/rings/:id' => "rings#show", :as => :ring
First off, I'm using rails 3.0.8 with friendly_id 3.2.1.1.
I'd like to be able to view the posts at website.com/:title, so drop the "/posts".
But I'd also like to have an /admin view. From there, a user should be able to create/edit/destroy posts. I already have a admin.html.erb view with links to various actions.
Right now my routes file look like:
MyApp::Application.routes.draw do
root :to => 'posts#index'
resources :posts
match "/:id" => "posts#show"
match "/admin" => "posts#admin"
end
This works for website.com/:title, but for website.com/admin I get an error:
Couldn't find Post with ID=admin
.... which makes sense. But I'm not sure how to solve this problem.
The rules are run through top to bottom. So put your admin rule on top of the resource definition.
If you put /admin first then it will work (as noted by cellcortex). You can also use :constraints if you can neatly separate your :id from 'admin'; for example, if your :id values are numeric, then something like this should work:
match '/:id' => 'posts#show', :constraints => { :id => /\d+/ }
match '/admin' => 'posts#admin'
In a simple case like yours, putting things in the right order will work fine. However, if your routing is more complicated, then the :constraints approach might work better and avoid a some confusion and chaos.
Use this
resource :posts, :path => '/'
with this all of your article will be directly under root
So in Posts Class you may add this:
def to_param
"#{id}-#{title.parameterize}"
end