I am using devise with few changes to the controllers and have the following routes defined.
devise_for :users, skip: [:registrations, :sessions]
devise_scope :user do
resource :registration,
only: [:new, :create, :edit, :update],
path: 'users',
path_names: { new: 'sign-up' },
controller: 'registrations',
as: :user_registration do
get :cancel
end
get 'sign-in' => 'devise/sessions#new', :as => :new_user_session
post 'sign-in' => 'devise/sessions#create', :as => :user_session
delete 'sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
get "sign-up" => "registrations#new", :as => :new_user_registration
end
resulting in the following duplicate routes:
new_user_registration GET /users/sign-up(.:format) registrations#new
new_user_registration GET /sign-up(.:format) registrations#new
How do i clean my route file that the duplication does not occur.
not sure, postet as answer for format:
I think that the redundancy cames from here:
devise_scope :user do
resource :registration,
only: [:new, :create, :edit, :update],
path: 'users',
path_names: new: 'sign-up' ,
this makes the
new_user_registration GET /users/sign-up(.:format) registrations#new
and
get "sign-up" => "registrations#new", :as => :new_user_registration
adds
new_user_registration GET /sign-up(.:format) registrations#new
so leave one of them
Related
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.
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.
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.
I have a problem with edit_user_path(user) in my rspec file:
it "should forward to the requested page after signin" do
user = Factory(:user)
visit edit_user_path(user)
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
response.should render_template('users/edit')
end
I get this error:
Failure/Error: visit edit_user_path(user)
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"users", :locale=>#<User id: 1, name: "Max Mustermann", email: "mel#example.com", encrypted_password: "1b0befc5caf12c2abf53d99sdfsd5ba15ebe28362eae21b2fb3...", salt: "331ca07444f5dcee76605bsdfasd95c833b7d5135b36a253723...", created_at: "2011-02-03 18:54:34", updated_at: "2011-02-03 18:54:34">}
I have no clue why it uses the user as :locale and why there is no :id with the user.id. I use :locale to set the language, e.g. localhost:3000/en/home or localhost:3000/de/home.
This is my routs.rb:
Hoapp::Application.routes.draw do
scope '(:locale)', :locale => /de|en/ do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :hotels, :only => [:create, :destroy]
root :to => 'pages#home'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/blog', :to => 'pages#blog'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
match '/overview', :to => 'pages#overview'
match '/preview', :to => 'pages#preview'
match '/settings', :to => 'pages#settings'
match 'm', :to => 'mobile#home'
match 'apartments', :to => 'mobile#apartments'
match 'apartment_info', :to => 'mobile#apartment_info'
match 'apartment_images', :to => 'mobile#apartment_images'
match 'apartment_price', :to => 'mobile#apartment_price'
match 'wellness', :to => 'mobile#wellness'
match 'test', :to => 'mobile#test'
scope "m" do
resources :requests, :only => [:new, :create]
match 'request_sent', :to => 'requests#sent'
end
match ':vanity_url/', :to => "mobile#home"
end
Ok I found a way to fix this in the tests (cf. https://github.com/rspec/rspec-rails/issues/255)
I put this code in spec_helper.rb
class ActionView::TestCase
class TestController
def default_url_options
{:locale => 'en'}
end
end
end
Maybe url_for is unable to infer the right parameters when you use a locale? Try it this way:
visit edit_user_path(:id=>user.id)
Including this in spec_helper.rb worked for me:
config.before do
default_url_options[:locale] = I18n.default_locale
end
I tried to put this in config/environments/test.rb and worked for me:
config.action_controller.default_url_options = { locale: I18n.locale }
sessions_controller.rb
def create
if user = User.authenticate(params[:login], params[:password])
session[:user_id] = user.id
redirect_to posts_path
else
render :action => 'new'
end
end
routes.rb
get "sessions/create"
get "sessions/destroy"
get "sessions/new"
resources :posts
resource :session
resources :users
match '/login', :to => 'sessions#new', :as => 'login'
match '/logout', :to => 'sessions#destroy', :as => 'logout'
Is it possible to keep the /login url after the render :action => "new" ???
thanks.
redirect_to '/login' does not keep the post information like render 'new' does.
I'm not completely happy with this solution, but this is what I have done:
resource :session, :only => [:create, :new, :destroy],
:path_names => { :new => 'login' }
Which gives you the following routes:
session POST /session(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /session/login(.:format) {:action=>"new", :controller=>"sessions"}
DELETE /session(.:format) {:action=>"destroy", :controller=>"sessions"}
The easy solution would be to simply change render :action => 'new' to redirect_to '/login'. I'm not amazingly fond of this, but it should solve the problem for you.