Rails.application.routes.draw do
resources :items
root 'items#index'
get 'items/index', to:'items/index'
get 'items/show', to:'items/show'
get 'items/new', to:'items/new'
get 'items/edit', to:'items/edit'
post '/items/create', to:'items/create'
post '/items/update', to:'items/update'
post '/items/destroy', to:'items/destroy'
get '/users/userindex', to: 'users/userindex'
get '/users/usershow', to: 'users/usershow'
get '/users/usernew', to: 'users/usernew'
end
ArgumentError: Missing :controller key
The notation used to map the routes to a controller's action is controller#action, not controller/action. Change your routes accordingly
Rails.application.routes.draw do
resources :items
root 'items#index'
get 'items/index', to:'items#index'
get 'items/show', to:'items#show'
get 'items/new', to:'items#new'
get 'items/edit', to:'items#edit'
post '/items/create', to:'items#create'
post '/items/update', to:'items#update'
post '/items/destroy', to:'items#destroy'
get '/users/userindex', to: 'users#userindex'
get '/users/usershow', to: 'users#usershow'
get '/users/usernew', to: 'users#usernew'
end
Moreover, you should take a look at Resourceful Routing. You have most of the routes declared wrongly. In other words, they wouldn't be needed when you already have them with resources
Related
I have this route:
resources :posts do
resources :comments
end
A post can be a "User Message" showed at:
/posts
A post can be a "News" showed at:
/news
How can I create the "/news" url inside the routes.rb file and pass a param for know inside the post controller the type of post I want?
This way you can declare routes which can not be declared using resources.
get '/news' => 'posts#index', as: :news
I'm having some troubles with routes in Ruby on Rails v5.2.0
Currently, I have a resource called users, so that I have a controller which takes actions (for example index) whenever I start my server in localhost on port 3000 and type in my browser
localhost:3000/users/
Is there an easy way to map the requests for this resource to the app root?Basically, I'm trying to achieve this:
localhost:3000/users/ --> localhost:3000/
localhost:3000/users/new/ --> localhost:3000/new/
This is how my routes.rb file looks like right now:
Rails.application.routes.draw do
devise_for :users
get 'landing/index'
get 'welcome/index'
resources :users
root to: 'landing#index'
end
Add the following lines to your routes.rb file
Change
root to: 'landing#index'
to
root "users#index"`
and add the line
get "/new" => "users#new"
Also if you want to learn more on routing, here is the link
http://guides.rubyonrails.org/routing.html
TLDR - Rails doesn't have a root model generator for routing
You can manually create the individuals routes
get :new, to: "users#new", as: "new_user"
...
However while using the rails generators resources you are just specifying a shorthand for
scope :model do
get :new, to: "model#new", as: "new_model"
...
end
You can checkout the rails guide to routing for more specifics on explicit creation
http://guides.rubyonrails.org/routing.html
HACKY SOLUTION
root to: "users#index", as: "users"
get :new, to: "users#new", as: "new_user"
post "/", to: "users#create"
scope ":id" do
root to: "users#show"
get :edit, to: "users#edit", as: "edit_user"
patch "/", to: "users#update"
...
end
It looks that what you want is to 'mute' users from the url. An option for this is to call path: '' on users like this:
Rails.application.routes.draw do
devise_for :users
get 'landing/index'
get 'welcome/index'
resources :users, path: '' # <-- HERE
root to: 'landing#index'
end
The value you give to path: is going to replace the resource name.
In this scenario users is being replaced with an empty string '', but it could be any other string.
This will remove users. However, you must consider that root to: 'landing#index AND users#index are both pointing to localhost:3000/
Without knowing your app, an option to solve this scenario, could be to have landing#index as root for gustes (not authenticated users) and users#index as a root for authenticated users.
I want to access the following path without the controller name
e.g. SITE/about SITE/faq
How to do it ?
routes.rb
get 'welcome/index'
get 'welcome/about'
get 'welcome/brand'
get 'welcome/product'
get 'welcome/news'
get 'welcome/faq'
get 'welcome/download'
get 'welcome/contact'
Specify a how you want the url to be and which controller and controller method to visit.
Example, in your routes.rb
get 'about', to: 'pages#about'
This way, example.com/about will point to action about in PagesController
Try this one:
scope '/', controller: 'welcome' do
get :index
get :about
get :brand
get :product
get :news
get :faq
get :download
get :contact
end
It will also allow you to forget about writing controller name in each line.
Hi i have admin panel controller and have many controller in admin panel.
I want to match routes usually without namespace i've used
match ':controller(/:action(/:id))', :via => [:get, :post]
I want this in namespace controller my current
router.rb
namespace :admin do
get '', to: 'dashboard#index', as: '/'
get 'dashboard/index'
##AUTHENTICATION
get 'login/index'
get 'login/logout'
post 'login/attempt_login'
get 'login/attempt_login'
##PAGES
get 'pages/index'
get 'pages/add_new'
get 'pages/edit'
post 'pages/create'
post 'pages/update'
post 'pages/task'
get 'pages/task'
##USERS
get 'users/index'
get 'users/edit'
get 'users/delete'
get 'users/destroy'
get 'users/update'
get 'users/add_new'
post 'users/create'
post 'users/update'
post 'users/task'
#USER GROUPS
get 'user_group/index'
get 'user_group/add_new'
get 'user_group/edit'
post 'user_group/create'
post 'user_group/update'
post 'user_group/task'
#USER GROUPS
get 'access_sections/index'
get 'access_sections/add_new'
post 'access_sections/create'
post 'access_sections/update'
post 'access_sections/task'
end
Any solution please?
You simply wrap the routes you're declaring in a namespace like so:
namespace :login do
get 'index'
get 'logout'
end
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
For example we have orders which can be cancelled:
Following description in routes.rb
resources :orders do
post :cancel, to: 'orders/cancellations#cancel'
end
Will send request to app/controllers/orders/cancellations_contoller.rb
module Orders
class CancellationsController
def cancel
#order = Order.find(params[:id]).cancel
end
end
end
It's useful to refactor controller resourses with many service methods.
Wish it helps
What is the equivalent index path for resources :topics if I were to write it out manually?
When I run rake routes with the resources line in the routes.rb file it shows.
GET /topics(.:format) {:action=>"index", :controller=>"topics"}
I have tried a few things with no success, for example:
get 'topics/index' #=> 'topics#index'
as the route says:
get "/topics" => "topics#index", :as => :topics
you can now use topics_path or topics_url