Rails 3 Routing question - ruby-on-rails-3

I have an issue where I have an admin subdomain setup in the routes like so:
constraints :subdomain => 'admin' do
scope :module => "admin" do
match 'articles/:id/', :to => 'articles#show'
resources :articles, :events do
collection do
post :update_attribute_on_the_spot
end
end
root :to => "dashboard#index"
end
end
Then after that on articles for the main site I have:
resources :articles, :events, :george
match '/:year/:month/:day/:slug', :to => 'articles#show', :as => "article", :constraints => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ }
match '/event/:year/:month/:day/:slug', :to => 'events#show', :as => "event", :constraints => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ }
I am wondering how I make sure the main site routes are not used when the admin subdomain routes are in effect, as of now when going to the admin section articles show is mapped to the main site route and therefore the admin routes will not function unless that route is removed.
If anyone can show me the best way around this issue it would be great.
Thanks!

I did not realize declaring something with :as would overwrite any other route so simply:
match '/:year/:month/:day/:slug', :to => 'articles#show', :constraints => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ }
Fixed everything!

Related

how to make user defined routes in rails

i have one doubt in ruby on rails...i have a problem in routes file.i am working for 1 week.but i am not getting a appropriate result.my problem is. i am working on a project called canvas which is a online tutorial.In that project in the course home page it have a side menu called pages.when clicking that it goes to the url. The canvas version is rails 2
http://localhost:3000/courses/1/wiki/front-page
and its routes file for the page is
concern :wikis do
get 'pages' => 'wiki_pages#pages_index'
get 'pages/:wiki_page_id' => 'wiki_pages#show_page', :wiki_page_id => /[^\/]+/, :as => :named_page
get 'pages/:wiki_page_id/edit' => 'wiki_pages#edit_page', :wiki_page_id => /[^\/]+/, :as => :edit_named_page
resources :wiki_pages, :path => :wiki do
match 'revisions/latest' => 'wiki_page_revisions#latest_version_number', :as => :latest_version_number
resources :wiki_page_revisions, :path => :revisions
end
match 'wiki/:id' => 'wiki_pages#show', :as => :named_wiki_page, :id => /[^\/]+/
end
my task is to add two more links to side menu named as faq and career without controller and view. i have put the routes for faq and career routes as below:
concern :faq do
get 'faq' => 'wiki_pages#pages_index'
get 'faq/:wiki_page_id' => 'wiki_pages#show_page', :wiki_page_id => /[^\/]+/, :as => :named_page
get 'faq/:wiki_page_id/edit' => 'wiki_pages#edit_page', :wiki_page_id => /[^\/]+/, :as => :edit_named_page
resources :wiki_pages, :path => :wiki do
match 'revisions/latest' => 'wiki_page_revisions#latest_version_number', :as => :latest_version_number
resources :wiki_page_revisions, :path => :revisions
end
match 'faq/:id' => 'wiki_pages#show', :as => :named_faq_page, :id => /[^\/]+/
end
the career routes is
concern :career do
get 'career' => 'wiki_pages#pages_index'
get 'career/:wiki_page_id' => 'wiki_pages#show_page', :wiki_page_id => /[^\/]+/, :as => :named_page
get 'career/:wiki_page_id/edit' => 'wiki_pages#edit_page', :wiki_page_id => /[^\/]+/, :as => :edit_named_page
resources :wiki_pages, :path => :wiki do
match 'revisions/latest' => 'wiki_page_revisions#latest_version_number', :as => :latest_version_number
resources :wiki_page_revisions, :path => :revisions
end
match 'career/:id' => 'wiki_pages#show', :as => :named_wiki_page, :id => /[^\/]+/
end
using the above wiki_controller i want to change the url like below
when i clicking the faq the url must like this:
http://localhost:3000/courses/1/faq/front-page
but the actions are wikipages_index and show.
when i clicked the career link it must be.
http://localhost:3000/courses/1/career/front-page
like this the url must come for all index and show,edit and update.plz apologize me that it is not an standard english.can anybody help me.
thanks in advance
with regards
saran

Using Omniauth on Rails, how do I change the URL?

Currently I have these two routes in my rake routes output:
user_omniauth_authorize
/users/auth/:provider(.:format)
devise/omniauth_callbacks#passthru {:provider=>/facebook|twitter/}
user_omniauth_callback
/users/auth/:action/callback(.:format)
devise/omniauth_callbacks#(?-mix:facebook|twitter)
What file do I have to change to customize these so the route can read:
user_omniauth_authorize
/admin/manage/:slug/auth/:provider(.:format)
devise/omniauth_callbacks#passthru {:provider=>/facebook|twitter/}
user_omniauth_callback
/admin/manage/:slug/auth/:action/callback(.:format)
devise/omniauth_callbacks#(?-mix:facebook|twitter)
Add the following to your routes.rb file:
devise_for :users, :skip => :omniauth_callbacks
devise_scope :user do
match "/admin/manage/:slug/auth/:provider",
:constraints => { :provider => /facebook|twitter/ },
:to => "devise/omniauth_callbacks#passthru",
:as => :user_omniauth_authorize,
:via => [:get, :post]
match "/admin/manage/:slug/auth/:action/callback",
:constraints => { :action => /facebook|twitter/ },
:to => "devise/omniauth_callbacks",
:as => :user_omniauth_callback,
:via => [:get, :post]
end
Now, this is untested. I copied it from Devise's source code. So there are a couple of problems:
You have to add user to the :as alias, so it's not dynamic.
You have to add the auth providers to the constraints, so it's not dynamic.

Usernames in route with devise

I'm struggling to get usernames to show in the routes related to devise.
How do I get the following routes to show?
www.blabla.com/username/edit
www.blabla.com/username/event/id
Do I have to declare a scope within a scope? Thanks for the help.
Right now my routes file looks like this
Weplanthings::Application.routes.draw do
resources :vendors
resources :venues
resources :events
get "profiles/index"
get "users/index"
get "users/show"
get 'tags/:tag', to: 'events#index', as: :tag
authenticated :user do
root :to => 'home#index'
end
match '/event/new', :to => 'events#new'
root :to => "home#index"
devise_for :users do
get "/login" => "devise/sessions#new"
match '/login', :to => 'devise/sessions#new'
get "/logout" => "devise/sessions#destroy"
match '/logout', :to => 'devise/sessions#destroy'
get "/edit" => "devise/sessions#edit"
match '/edit', :to => 'devise/sessions#edit'
scope ":username", :as => "user" do
#resources :users
match '/', :to => 'profiles#index'
match '/edit', :to => 'devise/sessions#edit'
match '/event/:id', :to => 'events#show'
end
end
My suggestion is for you to use the friendly_id gem.
It will make much easier to do what you want and just with a single line of code.

omniauth + devise "user/auth/facebook" magic route question

On a rails 3 app, I've got these routes defined:
devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :sign_out => 'logout' } do
get 'login' =>'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'registrations#new', :as => :new_user_registration
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
# catchall route to deal with routing errors
match "*path" => "error#index"
However, now when I go to log /user/auth/facebook, it routes me to the errors page...
My solution to this was to add constraints to the final match:
match "*path" => "error#index", :constraints => lambda {|req| !req.path.starts_with?("/users/auth/") }
This works, but I am wondering if there is a better way... ?
I think you are better off letting a 404 page handle routing errors. You only get a 500 error in development. It's a 404 in production. For example on my site: http://agmprojects.com/test. So i suggest using the 404.html in the public folder instead, rather than loading that controller. Out of curiosity, are you responding with a 404 http status code?

Rails 3: What does this route do?

Looking for a little helping understanding how this route would work?
New route:
resources :artists do
resources :users
end
entire routes
resources :artists do
resources :users
end
match 'auth/:provider/callback' => 'authentications#create'
resources :authentications
devise_for :admins
match '/admin' => 'RailsAdmin/Main#index'
devise_for :users, :controllers => {:registrations => 'registrations'} do
match '/users/change_password', :to => 'registrations#change_password'
match '/users/edit_account', :to => 'registrations#edit_account'
end
resources :posts do
member do
get :likers
end
collection do
get :search
end
end
resources :relationships, :only => [:create, :destroy]
resources :appreciations, :only => [:create, :destroy]
match '/a_json/:id', :to => 'artists#index'
match '/s_json/:id', :to => 'stores#index'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/blog', :to => 'pages#blog'
resources :users do
member do
get :following, :followers, :likes
end
end
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
match '/:id' => 'users#show', :constraints => {:id => /[^\/]+/}, :as => :global_user
root :to => "pages#home"
end
This would create nested routes, allowing you to use URLs like /artists/5/users/45, which would call UsersController#show with a parameter artist_id which was 5, and a parameter id which was 45. All of the other usual RESTful routes are also created "nested" under a single artist.
Rails actually has a tool for showing you what routes have been generated: just run rake routes to take a peek.