I have been trying to implement a vanity url for user profiles, based on the example here: Rails 3: Permalink public profile. I have replaced the 'id' with username:
def to_param
username
end
However this has caused issues with my other routes. I have set them so that they match the default sets of routes exactly, when running 'rake routes'.
get '/users/' => 'users#index', :as => :users
post '/users' => 'users#create'
get '/users/new' => 'users#new', :as => :new_user
get '/users/:id/edit' => 'users#edit', :as => :edit_user
patch '/users/:id' => 'users#update'
put '/users/:id' => 'users#update'
delete '/users/:id' => 'users#destroy'
# for vanity url
get '/:id' => 'users#show', :as => :user
With this setup, trying to access delete and update routes give me 'no route matches' error. What is the proper way to specify these, and / or should I be doing this a different way? Any help is appreciated.
I think it's interresting and more readable to keep the resources syntax in the routes.rb, except for the show, which you can rewrite to customize user_path :
resources :users, :except => [:show]
# 2 possibilities for the show url
get '/users/:id' => 'users#show' # can be removed if you don't want to keep /users/:id url
get '/:id' => 'users#show', :as => :user
But change the controller to find user by username instead of id, for example
def show
#post = Post.find_by_username(params[:id]) # instead of Post.find(params[:id])
# ...
end
Related
I followed this rails cast to create authentication for a rails project. My routes currently look like this:
Rails.application.routes.draw do
resources :messages
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
get "new_photo" => "users#edit", :as => "new_photo"
root :to => "users#new"
resources :users
resources :sessions
end
How to I edit this file so that the root will be pointing to "messages#new", if a user is logged in and "users#new" when no user is logged in? I tried many of the solutions on other pages, but they didnt work (they were probably for devise). Thanks for the help!
You'll probably want to handle this in your controller.
routes.rb
root :to => "users#new"
users_controller.rb
def new
return redirect_to new_messages_url if current_user
# normal controller code below...
end
This will redirect the logged_in user (current_user) to the new messages page if already logged in. I'm just assuming that current_user holds your user data, it may be different for your application.
write it in any home controler.
def set_roots
if current_user
redirect_to dashboard_home_index_path
else
redirect_to home_index_path
end
end
in routes.rb file
root :to => 'home#set_roots'
match "/find_roots" => "home#set_roots"
My goal is when user requests root (AND ONLY ROOT, other pages work fine) with something like:
http://domain.com/////
http://domain.com//////////////
http://domain.com//
app should 301 redirect user to one and only correct:
http://domain.com/
I'm using Webrick, rack-rewrite gem and planning to host it on Heroku.
Please remember that i'm new in Rails.
UPDATE ======
Rails: redirect all unknown routes to root_url
— this is totally different problem. They just redirect all unknown hosts to root. I have 404 working normally.
My problem is that any amount of slashes works as homepage and URL has all of those slashes. I'm doing it for SEO to get rid of duplicates of homepage. I want it to work like here: http://netpeak.ua (try "netpeak.ua////////", you will be redirected to "netpeak.ua").
UPDATE 2 - added content of routes.rb ======
Rails.application.routes.draw do
resources :questions
resources :feedbacks
devise_for :users
get '' => 'public#index'
get '/agency' => 'public#about'
get '/contact' => 'public#contact'
get '/uslugi' => 'services#index'
get '/portfolio' => 'projects#index'
get '/uslugi/:id' => 'services#show'
get '/portfolio/:id' => 'projects#show'
resources :articles, param: :id
resources :settings, as: 'home' #home so it doesn't go to another page
namespace :admin do
resources :articles, :users, :projects, :services, :feedbacks, :questions
get '' => 'projects#index'
get 'contact' => 'settings#contact'
get 'feedback' => 'settings#feedback'
get 'fininfo' => 'settings#fininfo'
get 'calls' => 'settings#calls'
get 'orders' => 'settings#orders'
get 'letters' => 'settings#letters'
get 'allquestions' => 'settings#allquestions'
get 'projectsettings' => 'settings#projectsettings'
get 'servicessettings' => 'settings#servicessettings'
get 'aboutsettings' => 'settings#aboutsettings'
get 'startpagesettings' => 'settings#startpagesettings'
patch 'contact' => 'settings#update'
patch 'feedback' => 'settings#update'
patch 'fininfo' => 'settings#update'
patch 'projectsettings' => 'settings#update'
patch 'servicessettings' => 'settings#update'
patch 'aboutsettings' => 'settings#update'
patch 'startpagesettings' => 'settings#update'
end
end
I am using Rails 4 and am trying to include the koudoku stripe gem. Here is my routes:
# Added by Koudoku.
mount Koudoku::Engine, at: 'koudoku'
scope module: 'koudoku' do
get 'pricing' => 'subscriptions#index', as: 'pricing'
end
resource :account
devise_for :users, :skip => [:sessions]
as :user 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
get '/dashboard', to: 'dashboard#index'
get '/reports/generate', to: 'reports#generate'
authenticated :user do
root :to => 'dashboard#index', :as => :authenticated_root
end
root :to => redirect('/login')
And this is the error I am getting:
undefined local variable or method `root_url
I can access the other routes just fine, it is just trying to render the Application Helper methods (for instance, a custom app method I have defined, or routes methods) from the module routes... Does this make sense? How do I fix this?
Try adding "main_app." before your root path. For example:
main_app.root_path
Conditional logic in the routing layer kind of goes against the intent of the Rails MVC architecture. The route file should just map a web request to a controller, which then has conditional logic to determine what is displayed.
In this case it's a bit different since you want to redirect, but I personally would still put it in the controller. In other words send the root to dashboard#index, and then at the top of that controller (or in a before_filter) just do
redirect_to login_path unless current_user_authenticated?
(here I'm assuming you would have a named route for login, which would be good practice, as well as a current_user_authenticated? method to check whatever logic you want before the redirect. This would be a more Rails-y approach, whatever that's worth...)
I need help. I want administration for my rails application. I tried to set the routes with namespaces, but namespaces require a resource, and resource must have id in get request.
Anybody know how to set up correctly? I using windows machine. Thanks.
My routes :
Web::Application.routes.draw do
namespace :admin do
resources :access # GET http://localhost/admin/access/login/login - stupid??
end
match ':controller(/:action(/:id))(.:format)'
end
Try to use resource :access instead of resources :access
namespace :admin do
resource :access
end
It will generate routes:
admin_access POST /admin/access(.:format) admin/access#create
new_admin_access GET /admin/access/new(.:format) admin/access#new
edit_admin_access GET /admin/access/edit(.:format) admin/access#edit
GET /admin/access(.:format) admin/access#show
PUT /admin/access(.:format) admin/access#update
DELETE /admin/access(.:format) admin/access#destroy
namespace :admin do
get "login" => "access#login", :as => :login # GET http://localhost/admin/login - admin_login_path
end
If you don't have a set of restful resources, but just want a set of different controller methods, here's one way you can do it:
scope '/admin' do
get '' => "admin#index", :as => 'admin_home'
get '/users' => 'admin#users', :as => 'admin_users'
get '/other_admin_task' => 'admin#other_admin_task', :as => 'other_admin_task'
end
I already have a route to match /username for the users show page. However, when I add another action, for example: followers and following as below:
resources :users, :only => [:show] do
get :following, :followers
end
I get the URL /users/username/following instead of /username/following.
How can I make all the /users/username URL's be matched as /username/etc.. ?
Here's my /username route:
match '/:id' => 'users#show', :constraints => { :id => /[a-zA-Z0-9\-_]*/ }, :as => "user_profile"
thank you.
Edit:
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
Near the bottom
match ':id/:action', :controller => :users