I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
namespace namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin. That's what admin/admin#index means. :)
According to rubyonrails.org guide on routing you should be able to do something like this.
it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
on rails 4 in routes two roots not permited now
use
get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index and other methods to be in straightforward AdminController controller in usual directory, views to go in views/admin etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end
Related
I am new to Ruby on Rails. I want to have following structure for admin section.
app/controller/admin/admin_controller.rb and all other admin section controller under app/controller/admin/ folder
app/views/layout/admin/admin.html.erb to keep separate html layout for admin section
At the same time i want to use Devise Gem for admin and front end user authentication.
I executed rails g devise:views admin, rails generate devise Admin and rails g controller admin/home index command that created views, model and controller for admin user. Now what routes and other setting i need to add so that ruby could understand that if i type http://localhost:3000/admin/ then i should be redirected to http://localhost:3000/admins/sign_in/ page and after entering correct admin credentials i should redirected to index method of controllers/admin/home_controller.rb
is it also possible to keep singular convention of Devise admin views like admin/sign_in instead of admins/sign_in ?
I have searched a lot but could not get relevant help. Please provide steps to achieve above.
Thanks in advance.
This is how route file looks like
Rails.application.routes.draw do
namespace :admin do
get 'home/index'
end
devise_for :admins
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "home#index"
end
When i type http://localhost:3000/admin/ then i get below error
Your problem is that you do not have root route defined for /admin.
I have the same URL convention routes in one of the apps and routes.rb looks like this:
Rails.application.routes.draw do
# Admin part
devise_for :admins, path: '/admin'
scope module: :admin, path: '/admin', as: 'admin' do
root to: 'home#index'
end
# Redirect app root to client part
root to: redirect(path: '/panel', status: 301)
# Client part
devise_for :clients, path: '/panel'
scope module: :panel, path: '/panel', as: 'panel' do
...
end
end
When I hit this link:
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
I get this error:
uninitialized constant InvitationsController
The method in InvitationsController looks like this:
def sumbit_invitation
#invi = #invitations.find(params[:id])
#invi.update_attributes(accepted: true)
end
and routes is:
resources :invitations do
put :sumbit_invitation
end
I propose you can follow the below steps to resolve your problem:
1/ run command rake routes to make sure your routes are correct?
In my opinion, it should be:
resources :invitations do
member do
put :sumbit_invitation
end
end
2/ Make sure you URL is correct
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
should be changed to
link_to("Accept", invitations_sumbit_invitation_path(invitations), method: :put)
Using rake routes to get correct path
4/ Make sure that your controller InvitationsController worked
P/S: It is better if you share your routes.rb file or logs rake routes
You controller is in the module ControlPanel and lives in the control_panel subfolder of your app's controller's folder, but your route doesn't have a corresponding controll_panel namespace defined.
Add a controll_panel namespace to your routes:
namespace :controll_panel do
resources :invitations do
put :sumbit_invitation
end
end
This will make the invitation_sumbit_invitation_url invalid and you will have to change it to something like admin_invitation_sumbit_invitation_url or invitation_sumbit_admin_invitation_url. From the top of my head I am not sure how Rails handles the naming. Use rake routes in your console to get a list of all available route names in your app.
I this context I advise to read: The Rails Guide: Routing
I'm trying to learn Ruby on Rails, from here: http://guides.rubyonrails.org/getting_started.html , But when I try to set the root for the route up in Section 4.3, although the route is recognized, when I run the server and localhost:3000, it still thinks there's no root route. What could the problem be? I'm using Ruby 1.9.3.
Routes
Without your routes file, or even the error which is being presented, I'll just give you what you need to get the "root" route working:
#config/routes.rb
root "application#index" #-> syntax - "controller#action"
This has to be backed up with the appropriate controller action:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def index
end
end
It also needs the appropriate views:
#app/views/application/index.html.erb
This is the root page!
i have a simple cms on ROR 3.2.
with this folder scheme:
app |controllers |my controllers
but i wanted to have an "admin" section where i could have some controllers too.
so i created
rails generate controller admin/Users
app | controllers |admin & my admin controllers
so my file is:
users_controller.rb
class Admin::UsersController < ApplicationController
def index
render(:text => "sou o index!")
end
def list
render(:text => "sou o list")
end
end
On my routes i have:
namespace :admin do
resources :users
end
match ':controller(/:action(/:id))(.:format)'
Im new to rails and i cant figure out the solution. Cant find it anywhere.
The PROBLEM is
i try do acess:
http://localhost:3000/admin/users/list
and i get this error:
Unknown action The action 'show' could not be found for
Admin::UsersController
You seem to not have an understanding of how Rails's RESTful routing works by default. I recommend reading the Resource Routing section of the Rails Guides. By default, when using resources in your routes, the show action is what is used to display a particular model record. You can customize this behavior to an extent in that you can change the URL that for the show action, but not the method name in the model:
resources :users, :path_names => { :new => 'list' }
If you are going to use RESTful routing (which you should), you should remove the default route (match ':controller(/:action(/:id))(.:format)'). Also, you can run rake routes at any time from the terminal to see details about your current routing configuration.
Your on the right track, however, there are a few more steps involved to complete your solution for a backend admin CRUD section. Check out the following example of how to create it yourself:
https://stackoverflow.com/a/15615003/2207480
I am new to Ruby on Rails
I am getting this error
uninitialized constant WelcomeController
after creating the sample project. I enabled
root :to => 'welcome#index'
in routes.rb.
When you say
root :to => 'welcome#index'
you're telling Rails to send all requests for / to the index method in WelcomeController. The error message is telling you that you didn't create your WelcomeController class. You should have something like this:
class WelcomeController < ApplicationController
def index
# whatever your controller needs to do...
end
end
in app/controllers/welcome_controller.rb.
I'm very very new to Rails and also ran into this error while following along with Rails Tutorial by Michael Hartl. The problem I had was that in the config/routes.rb file, I just uncommented the root :to => "welcome#index":
# just remember to delete public/index.html.
root :to => "welcome#index"
but with the structure of the sample_app was that "welcome#index" should be 'pages#home' instead, since everything was originally set up through the "pages" controller.
root :to => 'pages#home'
It's even right there in the book, but I just overlooked it and spent quite a while afterwards trying to figure out where I went wrong.
Make sure WelcomeController is defined in a file called welcome_controller.rb
rails generate controller welcome index
If you not generate the page with name welcome, then just generate the page like: $ rails generate controller pagename index. So then into the: config->routes.rb you should edit root 'welcome#index' to root 'pagename#index'
Keep this if you want it to be your context root after you generate your welcome parts.
Rails.application.routes.draw do
root 'welcome#index'
end