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.
Related
I'm having some difficulty getting all the RESTful routes to be setup correctly. I've taken the simplest of simple steps in my app so far.
Create new rails app
Create new controller with RESTful actions
Add resources to routes.rb
And yet 4 out of my 7 actions fail the default tests. Can anyone please explain why?
rails generate controller Employees new create update edit destroy index show
And here's the corresponding controller, employees_controller.rb
class EmployeesController < ApplicationController
def new
end
def create
end
def update
end
def edit
end
def destroy
end
def index
end
def show
end
end
Now I add the resources to routes.rb
Rails.application.routes.draw do
resources :employees
end
All the routes seem to be there, based on the output of rake routes
Prefix Verb URI Pattern Controller#Action
employees GET /employees(.:format) employees#index
POST /employees(.:format) employees#create
new_employee GET /employees/new(.:format) employees#new
edit_employee GET /employees/:id/edit(.:format) employees#edit
employee GET /employees/:id(.:format) employees#show
PATCH /employees/:id(.:format) employees#update
PUT /employees/:id(.:format) employees#update
DELETE /employees/:id(.:format) employees#destroy
But now when I run the default tests, I get 4 errors:
# Running:
.EEEE..
Finished in 0.267712s, 26.1475 runs/s, 11.2061 assertions/s.
1) Error:
EmployeesControllerTest#test_should_get_update:
ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"employees"}
test/controllers/employees_controller_test.rb:15:in `block in <class:EmployeesControllerTest>'
2) Error:
EmployeesControllerTest#test_should_get_destroy:
ActionController::UrlGenerationError: No route matches {:action=>"destroy", :controller=>"employees"}
test/controllers/employees_controller_test.rb:25:in `block in <class:EmployeesControllerTest>'
3) Error:
EmployeesControllerTest#test_should_get_show:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"employees"}
test/controllers/employees_controller_test.rb:35:in `block in <class:EmployeesControllerTest>'
4) Error:
EmployeesControllerTest#test_should_get_edit:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"employees"}
test/controllers/employees_controller_test.rb:20:in `block in <class:EmployeesControllerTest>'
7 runs, 3 assertions, 0 failures, 4 errors, 0 skips
Lastly, here is the contents of employees_controller_test.rb. As I said, these are the default tests auto-created by Rails upon generation of the controller.
require 'test_helper'
class EmployeesControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
test "should get create" do
get :create
assert_response :success
end
test "should get update" do
get :update
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get destroy" do
get :destroy
assert_response :success
end
test "should get index" do
get :index
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
end
according to dimakura's answer you have to change some routes in your test
the update method is a put (or a patch for partial changes in rails4)
test "should get update" do
put :update
assert_response :success
end
and for your edit, delete and show routes, the id is missing
test "should get edit" do
get :edit, id: 2
assert_response :success
end
Your controller test probably looks like:
class EmployeesControllerTest < ActionController::TestCase
test "should get create" do
get :create
assert_response :success
end
# other tests not shown
end
You should change it to this:
class EmployeesControllerTest < ActionController::TestCase
test "should get create" do
post :create
assert_response :success
end
# other tests should be changed accordingly
end
Use patch or put method for update, and delete for delete actions.
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
I'm testing a nested controller and get the following error:
1) Checklists::ItemsController index action should render index template
Failure/Error: get :index, :checklist_id => checklist.id
ActionController::RoutingError:
No route matches {:checklist_id=>1, :controller=>"checklists/items"}
In the browser loading /checklists/1/items loads fine.
Am I missing something in the spec?
The routes:
resources :checklists do
resources :items, :controller => "Checklists::Items"
end
The controller located in namespaced folder (/app/controllers/checklists/items_controller.rb):
class Checklists::ItemsController < ApplicationController
respond_to :html, :json
def index
#checklist_items = #checklist.items
respond_with #checklist_items
end
end
The spec (/spec/controllers/checklists/items_controller_spec.rb):
describe Checklists::ItemsController do
let(:user) { Factory :user, :role => 'admin' }
let(:checklist) { Factory(:checklist) }
let(:checklist_item) { Factory(:checklist_item) }
before(:each) do
sign_in_to(controller, user)
Checklist.stub(:find => checklist)
end
it "index action should render index template" do
get :index, :checklist_id => checklist.id
response.should render_template(:index)
end
end
Update: Routes for checklist items
checklist_items GET /checklists/:checklist_id/items(.:format) {:action=>"index", :controller=>"Checklists::Items"}
POST /checklists/:checklist_id/items(.:format) {:action=>"create", :controller=>"Checklists::Items"}
new_checklist_item GET /checklists/:checklist_id/items/new(.:format) {:action=>"new", :controller=>"Checklists::Items"}
edit_checklist_item GET /checklists/:checklist_id/items/:id/edit(.:format) {:action=>"edit", :controller=>"Checklists::Items"}
checklist_item GET /checklists/:checklist_id/items/:id(.:format) {:action=>"show", :controller=>"Checklists::Items"}
PUT /checklists/:checklist_id/items/:id(.:format) {:action=>"update", :controller=>"Checklists::Items"}
DELETE /checklists/:checklist_id/items/:id(.:format) {:action=>"destroy", :controller=>"Checklists::Items"}
It turns out the solution to the problem was in the routes:
I changed
resources :items, :controller => "Checklists::Items"
to
resources :items, :controller => "checklists/items"
and it works now
I have the following routes in my app:
GET /admin/comments(.:format) {:controller=>"admin/comments", :action=>"index"}
admin_comments POST /admin/comments(.:format) {:controller=>"admin/comments", :action=>"create"}
new_admin_comment GET /admin/comments/new(.:format) {:controller=>"admin/comments", :action=>"new"}
GET /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"show"}
PUT /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"update"}
admin_comment DELETE /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"destroy"}
edit_admin_comment GET /admin/comments/:id/edit(.:format) {:controller=>"admin/comments", :action=>"edit"}
admin_approve_comment /admin/comments/approve/:id {:module=>"admin", :controller=>"admin/comments", :action=>"approve"}
admin_reject_comment /admin/comments/reject/:id {:module=>"admin", :controller=>"admin/comments", :action=>"reject"}
which is declared as:
namespace "admin" do
resources :comments
match '/comments/approve/:id' => 'comments#approve', :as => "approve_comment", :module => "admin"
match '/comments/reject/:id' => 'comments#reject', :as => "reject_comment", :module => "admin"
end
and a functional test like this:
context "a POST to :approve" do
setup do
comment = Factory(:comment)
sign_in Factory(:admin)
post :approve, :id => comment.id
end
should respond_with :success
end
However, when I run this I get:
test: a POST to :approve should respond with 200. (Admin::CommentsControllerTest):
ActionController::RoutingError: No route matches {:action=>"approve", :id=>339, :controller=>"admin/comments"}
What's wrong here? What stupid mistake am I making?
These routes look like member routes to me. So routing this way
namespace "admin" do
resources :comments do
member do
get :approve
get :reject
end
end
end
This will generate routes like /admin/comments/:id/approve . This is the rails way as far i know.
I think it's better to put match before resources. Because it's not check if it's good or not.
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"}