How can I have make auto redirecting every user who goes to mysite.com/ to mysite.com/features?
thanks
Set your root route to direct folks there (these are Rails 3 routes):
in config/routes.rb
root "content#features"
in app/controllers/contents_controller.rb
class ContentsController < ApplicationController
def features
end
end
That won't do a redirect, however. To do that, you'll need something like this:
in config/routes.rb
match "features" => "contents#features", :as => "features"
root "content#index"
in app/controllers/contents_controller.rb
class ContentsController < ApplicationController
def index
redirect_to features_url
end
def features
end
end
Related
In my Rails app I want to forward my users to their personal sign in page which is stored in a cookie :user_subdomain. So when a user goes to www.app.com/sign_in, s/he should be automatically forwarded to www.app.com/sign_in/mycompany.
How can this be achieved?
I would like to keep my sign_in_path helper method because it's sprinkled all over my app.
If I simply redirect the new action using redirect_to sign_in_path(:name => cookies[:user_subdomain]) I end up in a loop.
Thanks for any pointers.
# routes.rb:
get 'sign_in', :to => 'sessions#new'
# sessions_controller.rb:
class SessionsController < ApplicationController
def new
params[:name] ||= cookies[:user_subdomain]
end
...
end
Then the solution is easy. You do not need to redirect, you just need optional parameter for your routes.
bound parameters
# routes.rb:
get 'sign_in(/:company_name)', :to => 'sessions#new'
# This will allow
# /sign_in
# and
# /sign_in/mycompany
# Both will lead to same action and you can keep your helper
# sessions_controller.rb:
class SessionsController < ApplicationController
def new
params[:company_name] ||= cookies[:user_subdomain]
# You logic here
end
...
end
I am trying make a project in which /username gets redirected to that username's profile. How can I make this happen?
The route would be: "get /:username", to: "users#profile"
You would change users#profile to whatever your controller action is called.
You need to make sure to put this at the end of your routes. Otherwise it will intercept all your routes.
For example, don't do the following:
get "/:username", to: "users#profile"
get "/foo", to: "pages#bar"
Because you will never be able to reach the pages#bar endpoint.
Problem with the previous answer is that anything that doesn't match in routes would be routed to users#profile.
Alternatively, and to solve that issue, you can create a dynamic router, like this:
class DynamicRouter
def self.load
Rails.application.routes.draw do
User.all.each do |user|
puts "Routing #{user.name}"
get "/#{user.name}", :to => "users#profile", defaults: { id: user.id }
end
end
end
def self.reload
Rails.application.routes_reloader.reload!
end
end
Then on the UsersController:
class UsersController < ApplicationController
def profile
#user = User.find(params[:id])
redirect_to not_found_path unless #user
end
end
And to actually generate the routes at server start:
Rails.application.routes.draw do
...
get 'not_found' => 'somecontroller#not_found', as: :not_found
DynamicRouter.load
end
Finally to reload routes when a user is added/updated:
class User < ActiveRecord::Base
...
after_save :reload_routes
def reload_routes
DynamicRouter.reload
end
end
Hope it helps!
We have a fully functional website but the backend is going down for maintenance, so we want to redirect all our customers to www.example.com/unavailable. Except for 1 page www.example.com/admin since this is the admin page where we put the unavailable page on and off.
So my question:
Is it possible to turn routes on and off like this:
Example code for routes.rb:
if Settings.unavailable
get "*", to: "/unavailable", except: "/admin"
end
With the help from the post from Michal, I've adapted his answer to the following:
class ApplicationController < ActionController::Base
before_filter :check_maintenance
def check_maintenance
if Settings.first.maintenance && request.fullpath.split("?")[0].gsub("/","") != 'unavailable' && (request.fullpath.split("?")[0].exclude? "admin") # or other conditions
redirect_to '/unavailable'
end
end
end
You can put conditions in your routes.rb but you won't be able to detect what is the current url. You should put this logic in before_filter in ApplicationController (or other top level controller):
class ApplicationController < ActionController::Base
before_filter :check_maintenance
def check_maintenance
if Settings.unavailable && action_name != 'unavailable' # or other conditions
redirect_to '/unavailable'
end
end
end
SingularI have this problem. I defined in route file my route:
namespace :admin do
root to: "home#index"
resources :define_user
end
I created users controller:
class DefineUsersController < ApplicationController
def create
...
end
def destroy
...
end
end
I created in views new folder 'define_users' with file 'show.html.haml'. I call it using link_to:
=link_to 'User', admin_define_user_path
And I get above error. I would like to stay with singular name. Thank for all answers.
You are trying to access show route without id of DefineUser object
= link_to 'User', admin_define_user_path(define_user)
Where define_user is an object of DefineUser class or id of this object
First of all, if you want to link_to some define_user, you have to provide object or id. Something like this: admin_define_user_path(#define_user).
To display all possible routes type rake routes in console.
Also resources should be in plural form.
Moreover you have to provide namespace in controller.
P.S.
As I see, it is a very bad idea to work with model called DefineUser. It's not a rails way. Just use User. DefineUser is a good name for method, but not model.
So, the best idea to handle your code:
routes.rb
namespace :admin do
root to: "home#index"
resources :users
end
users_controller.rb
class Admin::UsersController < ApplicationController
def index
...
end
end
view
= link_to 'Users', admin_users_path # for index
= link_to 'User', admin_user_path(#user) # for one user
Good idea to separate controllers by namespace. For example, you can have next structure:
application_controller.rb
admin #folder
L base_controller.rb
L users_controller.rb
L ..._controller.rb
So your base_controller should be inherited from application_controller
class Admin::BaseController < ApplicationController
layout 'admin_layout' # Different layout for all admin pages
Other controller in admin namespace will be inherited from base_controller
class Admin::UsersController < Admin::BaseController
def create
...
end
How do you setup your views, controllers and routes?
One controller for everything the control panel does, or many?
Firstly, let's try to think how we would view the various panels. Let's say our control panel is pretty simple. We have one panel to show all the users who have signed-up and can CRUD them, and another panel to show all of the images that have uploaded, and we can carry up CRUD on those too.
Routes:
scope path: 'control_panel' do
get 'users', to: 'panels#users', as: :panel_show_users
get 'photos', to: 'panels#photos', as: :panel_show_photos
end
Controller:
class PanelsController < ApplicationController
def users
#users = User.all
end
def photos
#photos = Photo.all
end
end
View file structure:
panels
|_ users.html.erb
|_ photos.html.erb
Okay, now I don't see any problems with that, to simply access the panels and populate the views with data. Do you see any problems?
Here is where I'm sort of at a cross roads though. What should I do when I want to Created Update and Delete a user/photo? Should I put them all in the PanelsController?
class PanelsController < ApplicationController
before_action :protect
def users
#users = User.all
end
def update_user
#user = User.find(params[:id])
#user.update(user_params)
end
def photos
#photos = Photo.all
end
def update_photo
#photo = Photo.find(params[:id])
#photo.update(photo_params)
end
private
def protect
redirect_to root_url, error: 'You lack privileges!'
end
end
While this would result in a large PanelsController, it would feel good to be able to execute that protect action and just one controller hook. It would also mean the routes would be easy to setup:
scope path: 'control_panel' do
get 'users', to: 'panels#users', as: :panel_show_users
post 'user', to: 'panels#update', as: :panel_create_user
get 'photos', to: 'panels#photos', as: :panel_show_photos
post 'photos', to: 'panels#photos', as: :panel_create_photo
end
I should use resource routes here?
Like I say, this will result in a huge panels controller, so I was thinking it may be better to have a separate controller for each resource and then redirect to panels views?
Routes:
scope path: 'control_panel' do
resources :users
resources :photos
end
Controllers:
class UsersController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def update
end
def destroy
end
end
class PhotosController < ApplicationController
def index
end
def show
end
def new
end
def create
end
def update
end
def destroy
end
end
Still some quirks though. I have my Users#index action there, but what if I have two routes that return an index of all users? In the control panel, but also, when people are searching for another user, for example. Should I have two routes in the User controller? def public_users and def control_panel_users? That may be the answer. Could setup a hook to run #users = User.all in both of them, but redirect to a different location, and not have the protect method redirect them.
How should I protect these routes from non-admins? Should I move my protect method into the the application controller? Wouldn't this be a bit fiddly to setup?
class ApplicationController < ActionController
before_action :protect
def protect end
end
class StaticController < ApplicationController
skip_before_action [:home, :about, :contact]
def home
end
def about
end
def contact
end
end
But that is my question. 1 control panel controller or no control panel controller.
I really wish there was more advanced tutorials out there :( Billions of books on CRUD, MVC and things, but nothing on advanced things like control panels and AJAX...
Don't have a control panel controller. And to protect stuff from non-admins, use namespacing - read more about it here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
You can protect your 'admin'-namespaced controllers with authentication, and have the non-namespaced controllers open to the public (or open to non-admin users)
With regards to your def public_users and def control_panel_users question, you could just have two def index methods - one in the non-namespaced controller, and one in the admin-namespaced controller. They would each do different things.
So, you'd have 4 controllers in total:
2 non-namespaced, one for users, one for photos (containing all public stuff)
2 admin-namespaced, one for users, one for photos (containing all control panel stuff)
If you wanted, rather than using 'admin' as the namespace, you could use some other term you prefer - like 'panel'. 'Admin' is pretty conventional though.