Rewriting Rails routes - ruby-on-rails

Currently I am working on a blog engine in RoR and I encounter severa issues with routing.
The routes.rb looks like this:
match '/admin', :to => 'posts#new'
match '/get/:id', :to => 'posts#get'
match '/new', :to => 'posts#new'
delete '/:id', :to => 'posts#destroy'
post '/edit/:id', :to => 'posts#update'
put '/edit/:id', :to => 'posts#update'
get '/edit/:id', :to => 'posts#new', :as => 'post'
get '/:slug', :to => 'posts#show', :as => 'post'
root :to => 'posts#index'
and I would like to transform it in something like:
resources :admin do
resources :posts
end
Any help would be very appreciated.

A bit more information is needed. What do you want to place in the admin resource? Only posting, or also editing?
But a few tips to get started:
- You have to split your posts-controller. Make a subfolder in the controllers called admin (the resource name). Move the admin-functions to this controller, and leave the public posts-function (index and show) in the normal posts_controller.
- Do the same for the views.
And, i suspect you want the routes to be:
namespace :admin
resources :posts
end
get '/:id', :to => 'posts#show'
root :to => 'posts#index'
Then you can put some form of authentication to the admin namespace.
Hope this helps you on the way.

Related

Devise: Issue with routing using devise_scope

I'm trying to route my User model sign up form to /sign_in. Tried several things and just used the example given in the devise docs.
I copied in to my routes.rb
devise_scope :users do
get "sign_in", to: "devise/sessions#new"
end
and rake routes says it should be working and shows what I expected.
http://s1.postimg.org/do335u8v3/Screen_Shot_2014_06_18_at_11_34_34.png
What's going wrong here?
Try this instead.
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
get "/users/sign_up", :to => "registrations#new"
end

devise won't redirect to after_sign_up_path when using customized devise routes

I invoke the after_sign_up_path_for(resource) by defining it in an inherited registrations_controller:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
new_user_profile_path(resource)
end
end
This redirect works fine if I don't customize the routes. however, when I do, it no longer works. My routes file has:
devise_for :users, skip: [:sessions], controllers: { registrations: "registrations" }
devise_scope :user do
get 'signin' => 'devise/sessions#new', as: :new_user_session
post 'signin' => 'devise/sessions#create', as: :user_session
delete 'signout' => 'devise/sessions#destroy', as: :destroy_user_session
get 'signup' => 'devise/registrations#new', as: :new_user_registration
post 'signup' => 'devise/registrations#create', as: :user_registration
root to: 'pages#home'
end
Specifically, the post 'signup' => 'devise/registrations#create', as: :user_registration line messes things up. If that's not there, it works fine. However, if that's not there, then when registration fails due to validation error, the URL defaults to /users rather than /signup, which is an undesired behavior.
Anyone know if this is something I am doing wrong or if there's a bug in devise?
The issue with your custom routes is that you are pointed back to Devise controllers:
post 'signup' => 'devise/registrations#create', as: :user_registration
That obviously is not going to work. Point to your own controller instead:
post 'signup' => 'registrations#create', as: :user_registration
Just briefly looking at your code provided, I don't think the route you want is new_user_profile_path. You probably want user_profile_path, can you run rake routes and paste the output here?

Having trouble routing to profile homepage, rails

I'm having trouble having the root url route to a signed in user's profile page.
This is my config file-
get "profiles/show"
resources :players
devise_for :users
devise_scope :user do
get 'register', to: "devise/registrations#new", as: :register
get 'login', to: "devise/sessions#new", as: :login
get 'logout', to: "devise/sessions#destroy", as: :logout
end
resources :logistics
resources :notes
root :to => 'notes#index'
get '/:id', to: 'profiles#show'
I want that 'root to' to show the 'profiles#show' view. Just as the get '/:id' route does right below it.
I've tried and researched what I thought would work to no avail. Thanks for your help on this.
Hey try something like
match "/:id" => "profiles#show"

Change rails route to params in path

Google bot wants to open my Ajax address like:
http://ots.wlasowy.ru/ru?_escaped_fragment_=/ru/pages/service
I have a record like match 'pages/service', :to => 'pages#service' in routes.rb.
What else I need to add to routes.rb?
My routes.rb:
scope "(:locale)", :locale => /en|ru/ do
match '/pages/news' => 'pages#news'
match '/articles/page/:page', :to => 'pages#news'
match '/pages/home' => 'pages#home'
resources :pages
resources :articles
resources :vacancies
mount Ckeditor::Engine => '/ckeditor'
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
devise_for :users
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
end
end
Add your route.rb, view files. You have some bugs in your code.

Rails 3.1 subdomain based controller routing

I'm currently getting the error:
No route matches [GET] "/tenant_admin"
I was using something like:
http://example.com/accounts/1/tenant_admin
but I'm now passing the account id as a subdomain;
http://AccountName.example.com/
Is it possible to make the url work like this:
http://AccountName.example.com/tenant_admin ?
Routes.rb
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :users
resources :sessions
resources :password_resets
resources :accounts do
resources :tenant_admin
end
constraints(Subdomain) do
match '/' => 'accounts#show'
end
root :to => "welcome#index"
You have to put your tenant routes under resources :accounts and constraints(Subdomain). I don't recommend using copy and paste but a lambda instead.
tenant_routes = lambda do
resources :tenant_admin
end
resources :accounts do
tenant_routes.call
end
constraints(Subdomain) do
tenant_routes.call
end

Resources