How can I remove slug name from URL? I have URL with parent page:
host_url/home/index
but I need this to be as
host_url/index #without parent slug name
You can simply change the routes as:
match '/index', :to => 'home#index', via: [:get]
If using resources in routes .
resources :users, path: '/'
get '/index' => 'home#index'
Related
I am having an issue with routes, have tried to google but nothing came out so far. Would be great, if someone will able to explain, how works and solution.
Thanks,
Cheers :)
Sample:
1)
default url: lvh.me:3000/restaurants
Default url works fine, but once adding any unknown subdomain.
adding unknown subdomain: blabla.lvh.me/restaurants
It still visits lvh.me/restaurants and url shows with subdomain.
ok, lets add known subdomain from route:
platform.lvh.me/restaurants
It still visits lvh.me/restaurants and url shows with subdomain.
The admin and platform subdomains are react the same way.
2)
The same thing happens on :mobile and other extra routes
Route:
namespace :admin, path: '/' do
root to: 'pages#index'
end
constraints subdomain: 'platform' do
namespace :platform, path: '/' do
resources :categories
root 'pages#index'
end
end
resources :restaurants
root 'pages#index'
end
Rails routes by default searches the given URL to find from all routes, no matter subdomain, constraints...
Sample:
default url: lvh.me/restaurants
if to use any subdomain admin.lvh.me/restaurants, It still finds and renders from default url. Admin subdomain dnt have restaurants url.
Solution:
Need to add after every subdomain: match '*a' => redirect(path: '/'), via: :get
or render other pages like 404/500: match '*a', :to => 'errors#not_found', via: :get
Otherwise, will go further and renders from default url.
namespace :admin, path: '/' do
root to: 'pages#index'
match '*a' => redirect(path: '/'), via: :get
end
constraints subdomain: 'platform' do
namespace :platform, path: '/' do
resources :categories
root 'pages#index'
match '*a' => redirect(path: '/'), via: :get
end
end
resources :restaurants
root 'pages#index'
match '*a' => redirect(path: '/'), via: :get
end
I can't set root path for subdomain.
This is my routes.rb part:
constraints subdomain: 'blog' do
scope :module => "blog", :as => "blog" do
resources :posts
end
end
root 'statics#index'
When I am visiting blog.example.com I've got static#index action response and get posts#index, when visiting blog.example.com/posts.
I want to set root path for blog.example.com pointing to posts#index.
No effect for this:
match '/' => 'posts#index', :constraints => { :subdomain => 'blog' }, via: [:get]
I believe you should still be able to call root within the constraints block:
constraints subdomain: 'blog' do
root :to => 'posts#index' # Perhaps this needs to be `blog/posts#index`?
scope :module => "blog", :as => "blog" do
resources :posts
end
end
root 'statics#index'
This works for me with separate namespaces for each subdomain.
I have a "list" model and "ListsController" controller for it. By default, the route for lists was /lists/1, /lists/1/edit/, etc. I changed my routes.rb file to make it so the show path was "/:id", the new path was "/new".
Here's my routes file:
ToDo::Application.routes.draw do
root to: 'pages#home'
match '/about', to: 'pages#about'
match '/contact', to: 'pages#contact'
match '/help', to: 'pages#help'
resources :lists
match '/new', to: 'lists#new'
match '/:id', to: 'lists#show'
match '/:id/new', to: 'lists#new_item'
end
I can access a list by doing "localhost:3000/1" perfectly fine. But now I'm trying to use link_to, and when I do "link_to "List", list", it generates a url to the original route, which is "localhost:3000/lists/1".
Does anyone know how to fix this? Is there anything I should be doing better with my routes?
Thanks!
Instead of using match you could simply provide alternative path for resources:
resources :lists, path: ''
You will need to specify as: 'name' option to create a named route for your match rules, and to overwrite the named route provided by resource :lists.
resource :lists
match '/new', to: 'lists#new', as: 'new_list'
match '/:id', to: 'lists#show', as: 'list'
When I request a URL as follows:
http://localhost:3000/password_resets/edit/4RghIKJNygEDswIuuCo
I'm not getting the [:id] parameter, i.e. 4RghIKJNygEDswIuuCo.
Here is my route file, are there any modifications required for this?
ActionController::Routing::Routes.draw do |map|
match 'primary', :to => 'pages#primary', :as => "primary"
match 'admins', :to => 'admin_users#list', :as => "admins"
match 'login', :to => 'user_sessions#new', :as => "login"
match 'logout', :to => 'user_sessions#destroy', :as => "logout"
root :to =>"public#index"
match 'HFA/:id/' => 'public#show'
match 'HFA/:id/:uid' =>'public#show'
match 'public/projectview/:projectid/' => 'public#projectview'
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
resources :users
resources :usertypes
resources :user_sessions
end
The default is /controller/:id/edit, following the REST architecture.
Do you really want to change this?
If so, verify the order of the declarations:
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action’s
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
and check your availables routes with the rake routes command:
http://guides.rubyonrails.org/routing.html#inspecting-and-testing-routes
if you put match instead of 'get' you will get all http (GET, POST, PUT, DELETE) route verbs
get 'password_resets/:id/edit', to: 'password_reset#create', as: :send_password_reset
the 'to:' defines the controller method, 'as:' defines the path name, 'get' defines the route verb and ':id' is the token created by this line in app/views/user_mailer/password_reset.text.erb
<%= send_password_reset_url(#user.password_reset_token) %>
run "rake routes" command and check the routes for "password_resets/edit/4RghIKJNygEDswIuuCo".
I just recently upgraded to 1.0.3, and the routes.rb file in my config/routes folder seems to ignore all my custom routes.
MY routes.rb
JollyStore::Application.routes.draw do
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'
root :to => 'pages#index'
namespace :admin do
resources :wysiwygs
end
match 'about_us/', :to => "pages#about_us"
match 'services/', :to => "pages#services"
match 'raw_resources/', :to => "pages#raw_resources"
match 'contact_us/', :to => "pages#contact_us"
match 'privacy_policy/', :to => "pages#privacy_policy"
match 'return_policy/', :to => "pages#return_policy"
match 'refund_policy/', :to => "pages#refund_policy"
match 'cancellation_policy/', :to => "pages#cancellation_policy"
match 'delivery_shipping_policy/', :to => "pages#delivery_shipping_policy"
end
If I run bundle exec rake routes, it returns all the approriate routes. But when I try to reach that specific page, I get :
undefined local variable or method `about_us_path'
Or the same error for every link that is within my custom routes. Somehow my routes are being ignored. Does anyone know a way to circumvent this issue?
I encountered the same error and found this solution, which solved it by prefixing main_app, before each of my_paths/_urls. In my case, these were links used in one of the /override.rb files.
So, try: main_app.about_us_path.
You can add new routes in the Spree using following block in routes.rb file
Spree::Core::Engine.routes.prepend do
# Your new routes
end
For me prepend did not work.
for me draw did the work:
Spree::Core::Engine.routes.draw do
resources :orders, except: [:new, :create, :destroy] do
post :my_order, on: :collection
end
end