Is it possible to reference app/controllers/admin/categories_controller.rb using categories_path instead of admin_categories_path ?
I'm using Rails 4.
# app/controllers/admin
class Admin::CategoriesController < Admin::BaseController
end
# visiting localhost:3000/admin/categories causes route not found error 'admin/categories'
scope module: "admin" do
resources :categories
end
# visiting localhost:3000/admin/categories causes uninitialized constant CategoriesController
scope "/admin" do
resources :categories
end
I believe that you have to reference the controller at the resources
scope 'admin' do
resources :categories, controller: 'admin/categories'
end
so the routes became
categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_category GET /admin/categories/new(.:format) admin/categories#new
edit_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
category GET /admin/categories/:id(.:format) admin/categories#show
PATCH /admin/categories/:id(.:format) admin/categories#update
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
Related
So I am trying to nest resources under a namespace, however when i try to navigate to the UserProfile new page I am hitting the following error:
ActionController::RoutingError at /users/xxxxxx/user_profiles/new
uninitialized constant AccountManagementPages::UserProfilesController
Did you mean? AccountManagementPages::UsersController
This is how the resources are set up in my routes.rb file
constraints(AccountManagement) do
namespace :account_management_pages, path: '' do
root to: 'users#new', as: :registration
resources :users, except: %w[index], path_names: { new: 'register' } do
resources :user_profiles
end
end
end
my file structure for both my controller and views are configured correctly (at least I thought they were).
And here is how my views are nested.
This is how I have my user_profiles_controller configured:
module AccountManagementPages
module Users
class UserProfilesController < ApplicationController
def show; end
def new; end
def edit; end
def create; end
def update; end
end
end
end
and my model associations (don't think this is overly relevant here but just incase it is.)
class User < ApplicationRecord
has_one :user_profile, dependent: :destroy
end
class UserProfile < ApplicationRecord
belongs_to :user
end
any help here would be greatly appreciated. Not sure why I am hitting this error?
Thanks in advance.
If you do rails routes, you'll get (amongst other things):
Prefix Verb URI Pattern Controller#Action
account_management_pages_registration GET / account_management_pages/users#new
account_management_pages_user_user_profiles GET /users/:user_id/user_profiles(.:format) account_management_pages/user_profiles#index
POST /users/:user_id/user_profiles(.:format) account_management_pages/user_profiles#create
new_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/register(.:format) account_management_pages/user_profiles#new
edit_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id/edit(.:format) account_management_pages/user_profiles#edit
account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#show
PATCH /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#update
PUT /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#update
DELETE /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#destroy
account_management_pages_users POST /users(.:format) account_management_pages/users#create
new_account_management_pages_user GET /users/register(.:format) account_management_pages/users#new
edit_account_management_pages_user GET /users/:id/edit(.:format) account_management_pages/users#edit
account_management_pages_user GET /users/:id(.:format) account_management_pages/users#show
PATCH /users/:id(.:format) account_management_pages/users#update
PUT /users/:id(.:format) account_management_pages/users#update
DELETE /users/:id(.:format) account_management_pages/users#destroy
As you can see, user_profiles is not nested under the users namespace. Rails, therefore, is expecting:
module AccountManagementPages
class UserProfilesController < ApplicationController
...
end
end
If you do:
constraints(AccountManagement) do
namespace :account_management_pages, path: '' do
root to: 'users#new', as: :registration
resources :users, except: %w[index], path_names: { new: 'register' } do
scope module: :users do
resources :user_profiles
end
end
end
end
...and then rails routes, you get (amongst other things):
Prefix Verb URI Pattern Controller#Action
account_management_pages_registration GET / account_management_pages/users#new
account_management_pages_user_user_profiles GET /users/:user_id/user_profiles(.:format) account_management_pages/users/user_profiles#index
POST /users/:user_id/user_profiles(.:format) account_management_pages/users/user_profiles#create
new_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/register(.:format) account_management_pages/users/user_profiles#new
edit_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id/edit(.:format) account_management_pages/users/user_profiles#edit
account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#show
PATCH /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#update
PUT /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#update
DELETE /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#destroy
account_management_pages_users POST /users(.:format) account_management_pages/users#create
new_account_management_pages_user GET /users/register(.:format) account_management_pages/users#new
edit_account_management_pages_user GET /users/:id/edit(.:format) account_management_pages/users#edit
account_management_pages_user GET /users/:id(.:format) account_management_pages/users#show
PATCH /users/:id(.:format) account_management_pages/users#update
PUT /users/:id(.:format) account_management_pages/users#update
DELETE /users/:id(.:format) account_management_pages/users#destroy
...and user_profiles will be nested under users. And you should be able to use:
module AccountManagementPages
module Users
class UserProfilesController < ApplicationController
...
end
end
end
I'm searching a reason why rake routes doesn't match the index path of my nested resource.
Here is my code:
namespace :api do
resources :photos do
resource :comments
end
end
Here is the result of the command: rake routes | grep comment
batch_action_admin_user_comments POST /admin/user_comments/batch_action(.:format) admin/user_comments#batch_action
admin_user_comments GET /admin/user_comments(.:format) admin/user_comments#index
POST /admin/user_comments(.:format) admin/user_comments#create
new_admin_user_comment GET /admin/user_comments/new(.:format) admin/user_comments#new
edit_admin_user_comment GET /admin/user_comments/:id/edit(.:format) admin/user_comments#edit
admin_user_comment GET /admin/user_comments/:id(.:format) admin/user_comments#show
PATCH /admin/user_comments/:id(.:format) admin/user_comments#update
PUT /admin/user_comments/:id(.:format) admin/user_comments#update
DELETE /admin/user_comments/:id(.:format) admin/user_comments#destroy
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
api_photo_comments POST /api/photos/:photo_id/comments(.:format) api/comments#create
new_api_photo_comments GET /api/photos/:photo_id/comments/new(.:format) api/comments#new
edit_api_photo_comments GET /api/photos/:photo_id/comments/edit(.:format) api/comments#edit
GET /api/photos/:photo_id/comments(.:format) api/comments#show
PATCH /api/photos/:photo_id/comments(.:format) api/comments#update
PUT /api/photos/:photo_id/comments(.:format) api/comments#update
DELETE /api/photos/:photo_id/comments(.:format) api/comments#destroy
I tried to add only: [:create, :index] to my comments resource but only the create route is visible.
According to the documentation about nested-resources I don't understand what's happening.
Thank you for your help.
It's because you are using a singular resource (resource :comments)
From the docs:
Sometimes, you have a resource that clients always look up without
referencing an ID. For example, you would like /profile to always show
the profile of the currently logged in user. In this case, you can use
a singular resource to map /profile (rather than /profile/:id) to the
show action
You'll need to use the standard resources method to get this working (resource omits the index action):
#config/routes.rb
namespace :api do
resources :photos do
resources :comments
end
end
My mistake. A "S" was missing on my resource.
namespace :api do
resources :photos do
resources :comments
end
end
Now it works.
Dear fellow Overflowers,
I am using Rails 2.3 and I have created a polymorphic Controller which is accessed by Views belonging to different namespaces. Here is the story and thanks for reading it in advance:
I have these routes:
rake routes | grep appointment
new_patient_appointments GET /patients/:patient_id/appointments/new(.:format) {:controller=>"appointments", :action=>"new"}
edit_patient_appointments GET /patients/:patient_id/appointments/edit(.:format) {:controller=>"appointments", :action=>"edit"}
patient_appointments GET /patients/:patient_id/appointments(.:format) {:controller=>"appointments", :action=>"show"}
PUT /patients/:patient_id/appointments(.:format) {:controller=>"appointments", :action=>"update"}
DELETE /patients/:patient_id/appointments(.:format) {:controller=>"appointments", :action=>"destroy"}
POST /patients/:patient_id/appointments(.:format) {:controller=>"appointments", :action=>"create"}
new_admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments/new(.:format) {:controller=>"admin/appointments", :action=>"new"}
edit_admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments/edit(.:format){:controller=>"admin/appointments", :action=>"edit"}
admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments(.:format) {:controller=>"admin/appointments", :action=>"show"}
PUT /admin/doctors/:doctor_id/appointments(.:format) {:controller=>"admin/appointments", :action=>"update"}
DELETE /admin/doctors/:doctor_id/appointments(.:format) {:controller=>"admin/appointments", :action=>"destroy"}
POST /admin/doctors/:doctor_id/appointments(.:format) {:controller=>"admin/appointments", :action=>"create"}
...these controllers:
Controllers/Admin/doctors_controller.rb
class Admin::DoctorsController < AuthorisedController
end
Controllers/appointments_controller.rb
class AppointmentsController < ApplicationController
end
Controllers/patients_controller.rb
class PatientsController < ApplicationController
end
...and these tests:
The relevant part in the tests:
test/functional/appointments_conrtroller_test.rb
require 'test_helper'
class AppointmentsControllerTest < ActionController::TestCase
fixtures :patients, :appointments, :doctors, :users
# The following passes:
def setup
login_as :admin
end
test "should show patient appointment" do
get :show, :id => patients(:one).to_param, :appointment_id => appointments(:app_one).id
assert_response :success
end
# The following fails, giving the error after the code block:
test "should show doctor appointment" do
get :show, :id => doctors(:one).to_param, :appointment_id => appointments(:app_one).id
assert_response :success
end
end
Error:
4) Error:
test_should_show_doctor_appointment(AppointmentsControllerTest):
ActionController::RoutingError: No route matches {:controller=>"appointments", :id=>"281110143", :action=>"show", :doctor_id=>2}
test/functional/appointments_controller_test.rb:55:in `test_should_show_doctor_appointment'
the test is under the base namespace, so as a next step, I created a test under Admin.
test/functional/admin/appointments_controller_test.rb
class Admin::AppointmentsControllerTest < ActionController::TestCase
fixtures :patients, :appointments, :doctors, :users
# The following passes:
def setup
login_as :admin
end
test "should show doctor appointment" do
get :show, :id => doctors(:one).to_param, :appointment_id => appointments(:app_one).id
assert_response :success
end
end
...and now I get this error:
1) Error:
test_should_show_doctor_appointment(Admin::AppointmentsControllerTest):
RuntimeError: #controller is nil: make sure you set it in your test's setup method.
test/functional/admin/appointments_controller_test.rb:13:in `test_should_show_doctor_appointment'
At this point, I added #controller = AppointmentsController.new under the setup method, only to get the very familiar:
1) Error:
test_should_show_doctor_appointments(Admin::AppointmentsControllerTest):
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"appointments", :doctor_id=>2, :id=>"281110143"}
test/functional/admin/appointments_controller_test.rb:14:in `test_should_show_doctor_appointments'
It seems like a vicious circle to me.
Thanks anyways...
pR
You should probably be doing this instead:
#controller = Admin::AppointmentsController.new
Otherwise you're referencing the controller inside the main namespace and not the Admin namespace.
I have a has_one relation:
# supplier.rb
has_one :presentation
...
# presentation.rb
belongs_to :supplier
...
and the folowing nested routes for them:
# routes.rb
resources :suppliers do
resource :presentation
end
running rake routesgives:
supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
GET ... {:action=>"show", :controller=>"presentations"}
PUT ... {:action=>"update", :controller=>"presentations"}
DELETE ... {:action=>"destroy", :controller=>"presentations"}
Why no name_helper for the show action?
I can override the problem doing something like:
resources :suppliers do
resource :presentation, :except => :show do
get "" => "presentations#show", as: "presentation"
end
end
giving the route:
presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}
but we all now that's not the right way to deal with it..
ANY SUGGESTIONS?
--
(edited)
supplier_presentation_path(#supplier)
does work, but why?... It doesn't appear when rake routes is executed on my shell...
I dont really know why it's not displayed when you do rake routes but did you try in your code to do supplier_presentation_path(#supplier)? It should work based on your routes.
Never the less it should work for you. Try this:
link_to "Presentation", [#suplier, #presentation]
or
link_to "Presentation", suplier_presentation_path(#suplier, #presentation)
Ok, so I am working on a blog application of sorts. Thus far, it allows for a user to sign up for their own account, create posts, tags, comments, etc.
I have just implemented the ability to use www.myapp.com/brandon to set #user to find by username and therefore correctly display the users information at each url. So when you go to www.myapp.com/brandon you see all Brandon's posts, tags, and comments associated with those posts, etc. Works great.
I'm implementing this URL mapping through the routes.rb file by adding the following:
map.username_link '/:username', :controller => 'posts', :action => 'index'
And then just setting the #user variable in the PostController and corresponding views to find_by_username. Now the issue is this. Once at www.myapp.com/brandon when you click on a post title, it sends to myapp.com/posts/id without the username in the URL.
How do I tell rails to replace the /posts with /username.
Is it even possible to insert the user_username variable into this code?
map.resources :posts, :as => [what goes here]
You said there's going to be more than just posts on the page? comments and tags too? Sounds like we need some resource aggregation here...
Another concern: what if a user picks the name faq and you want domain.com/faq down the road? You can't possibly know all the URLs you will want in the future. Prefixing paths with /profiles is a great way to build a little "namespace" to prevent this from happening. So...
Why not a ProfilesController?
script/generate controller profiles index show
routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :profiles, :only => [:index, :show] do |profile|
profile.resources :posts, :only => [:index, :show]
profile.resources :comments, :only => [:index, :show]
profile.resources :tags, :only => [:index, :show]
end
# ...
end
This will give you the following routes
profiles GET /profiles(.:format) {:controller=>"profiles", :action=>"index"}
profile GET /profiles/:id(.:format) {:controller=>"profiles", :action=>"show"}
profile_posts GET /profiles/:profile_id/posts(.:format) {:controller=>"posts", :action=>"index"}
profile_post GET /profiles/:profile_id/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
profile_comments GET /profiles/:profile_id/comments(.:format) {:controller=>"comments", :action=>"index"}
profile_comment GET /profiles/:profile_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
profile_tags GET /profiles/:profile_id/tags(.:format) {:controller=>"tags", :action=>"index"}
profile_tag GET /profiles/:profile_id/tags/:id(.:format) {:controller=>"tags", :action=>"show"}
profiles_controller.rb
class ProfilesController < ApplicationController
# show all profiles; profile browser
# /profiles
def index
end
# show one profile
# /profiles/:id
def show
#user = User.find_by_username(params[:id])
end
end
posts_controller.rb (and others)
class PostsController < ApplicationController
before_filter :find_profile, :only => [:index, :show]
# list all posts for this profile
# /profiles/:profile_id/posts
def index
end
# show one post for this profile
# /profiles/:profile_id/posts/:id
def show
end
protected
def find_profile
#user = User.find_by_username(params[:profile_id])
end
end
You should be able to create the link using:
= link_to "User Posts", subdomain_link_url(#user.username, #post)
In your PostController, then, I would use a before_filter to lookup and set the #user variable:
class PostController < ApplicationController
before_filter :find_user
def other_method
# Some code here
end
protected
def find_user
#user = User.find_by_username(params[:username])
end
end
I don't know much about routes and stuff, so forgive me if this doesn't make sense, but doesn't it works for you?
map.resources :posts, :path_prefix => '/:username' do |post|
post.resources :comments
end
I can see here that this will generate the following
posts GET /:username/posts(.:format) {:controller=>"posts", :action=>"index"}
POST /:username/posts(.:format) {:controller=>"posts", :action=>"create"}
new_post GET /:username/posts/new(.:format) {:controller=>"posts", :action=>"new"}
edit_post GET /:username/posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
post GET /:username/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
PUT /:username/posts/:id(.:format) {:controller=>"posts", :action=>"update"}
DELETE /:username/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
post_comments GET /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"index"}
POST /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"create"}
new_post_comment GET /:username/posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
edit_post_comment GET /:username/posts/:post_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
post_comment GET /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
PUT /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"update"}
DELETE /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}