Usernames in route with devise - ruby-on-rails-3

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.

Related

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 No method error issue in rails 3.1

I just created rails 3.1 app and updated devise 1.4.7.But When i browse http://localhost:3000/users/sign_up(as indicated in rake routes) ,I get " NoMethodError in Devise/registrations#new " extracted source is line
3: <%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>.
What is the solution to this?Thank you in advance.
routes.rb
Deals::Application.routes.draw do
devise_for :users
match "/users/sign_up" => "devise/registrations#new"
root :to => "home#index"
end
Use these lines in your routes.rb file.
root :to => "home#index"
devise_for :users
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
get "sign_up", :to => "devise/registrations#new"
end
Restarting the server did it for me. Try it before anything else!
Take out this part in config/routes.rb:
match "/users/sign_up" => "devise/registrations#new"
That's redundant, devise_for :users will add that route.
You can try devise_for :users followed directly by resources :users

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.

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!

Rails 3 Rspec 2 problem with :locale and RESTfull path

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 }