omniauth + devise "user/auth/facebook" magic route question - ruby-on-rails-3

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?

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.

Devise NameError in Devise/registrationsController#new

I followed the setting up devise tutorial, but now when I go to
/users/sign_up
I get
Devise NameError in Devise/registrationsController#new
same if I go to
/users/sign_in
I get
/NameError in Devise/sessionsController#create
I have scrapped my models for errors or typos. I don't really get it because it looks like it is having trouble with the "devise controllers" but there aren't any of those in my /app file, so I assume those files (like on git) are running somewhere in the background.
Any suggestions?
A bit late to answer, but my experience might help someone else out.
I had the same issue with devise & cancan. The solution in my case was simply to restart the server.
Try
devise_for :users, :controllers => {:sessions => 'devise/sessions'}, :skip => [:sessions] do
get '/login' => 'devise/sessions#new', :as => :new_user_session
post '/login' => 'devise/sessions#create', :as => :user_session
get '/logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
or
devise_for :users, :controllers => {:registrations => "devise/registrations"} do
get '/register' => 'devise/registrations#new', :as => :new_user_registration
end

How to achieve a separated Admin (namespaced or scoped) with two devise models (User & Admin) in Rails 3?

Consider this as a challenge rather than its general approach. The reason I mention this is because, it is generally preferred to incorporate admin-accessible features into the public facing site. This is what's required:
Devise model for Users, visitors accessing the public facing site
Devise model for Admins
Namespace or scope the admin 'area' to /admin. Admins can only login from this route.
Users can sign up directly from the site's public facing landing page; they are not forced to visit /users/sign_up as per the default devise generated route.
Consider overriding the default devise controllers
Thanks,
Mike.
The following seems like I've made some progress in the right direction; this at least provides identical out-of-the-box devise functionality for both users and admins, with the custom routing,
match 'admin', :controller => 'admin'
namespace :admin do
# to be updated later...
end
devise_for :users
#devise_for :admins, :path => "admin" # this works but uses the default
# Devise::SessionController
devise_for :admins,
:controllers => {
:sessions => "admin/sessions",
:passwords => "admin/passwords",
:registrations => "admin/registrations" }, :path => "admin",
:skip => [:sessions, :passwords, :registrations] do
get 'admin/sign_in' => 'admin/sessions#new', :as => :new_admin_session
post 'admin/sign_in' => 'admin/sessions#create', :as => :admin_session
get 'admin/sign_out' => 'admin/sessions#destroy', :as => :destroy_admin_session
get 'admin/sign_up' => 'admin/registrations#new', :as => :new_admin_registration
get 'admin/account' => 'admin/registrations#edit', :as => :edit_admin_registration
post 'admin/account' => 'admin/registrations#create', :as => :admin_registration
get 'admin/cancel' => 'admin/registrations#cancel', :as => :cancel_admin_registration
put 'admin/account' => 'admin/registrations#update'
delete 'admin/account' => 'admin/registrations#destroy'
post 'admin/password' => 'admin/passwords#create', :as => :admin_password
get 'admin/password/new' => 'admin/passwords#new', :as => :new_admin_password
get 'admin/password/edit' => 'admin/passwords#edit', :as => :edit_admin_password
put 'admin/password' => 'admin/passwords#update'
end
Ideas?
caveat: in this example, I've included the :registerable devise module in the Admin model just for testing during development. The sign_up route will, ultimately, be removed.
Much searching yielded (mind the pun) the following blog post that seems to indicate overriding a devise controller requires the re-mapping of all its specified 'HTTP verbs' as it were; this makes sense as unmapped ones would be handled by the default devise controller.
If anyone has more experience working with multiple devise models and the separated admin approach, I would be very much interested in your thoughts and suggestions!

Rails 3 Routing question

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!