Rails: Routing without plurals gives strange helpers - ruby-on-rails

I am getting a strange named helpers with this setup:
In config/routes.rb I have:
Qtl::Application.routes.draw do
resources :qtl_table do
collection do
get 'search'
end
end
...
end
rake routes outputs this:
search_qtl_table_index GET /qtl_table/search(.:format) {:action=>"search", :controller=>"qtl_table"}
qtl_table_index GET /qtl_table(.:format) {:action=>"index", :controller=>"qtl_table"}
POST /qtl_table(.:format) {:action=>"create", :controller=>"qtl_table"}
new_qtl_table GET /qtl_table/new(.:format) {:action=>"new", :controller=>"qtl_table"}
edit_qtl_table GET /qtl_table/:id/edit(.:format) {:action=>"edit", :controller=>"qtl_table"}
qtl_table GET /qtl_table/:id(.:format) {:action=>"show", :controller=>"qtl_table"}
PUT /qtl_table/:id(.:format) {:action=>"update", :controller=>"qtl_table"}
DELETE /qtl_table/:id(.:format) {:action=>"destroy", :controller=>"qtl_table"}
and I do have plurals turned off:
ActiveRecord::Base.pluralize_table_names = false
but I get this error:
undefined local variable or method `search_qtl_table_index' for #<#<Class:0x8056a3fa8>:0x8056a2338>
This is related to this question which I will delete soon:
Rails: routing and path helpers

This has nothing to do with pluralizing. YOu need to use search_qtl_table_index_path when you reference it rather than just search_qtl_table_index (you need to add the _path to the end).
So, your form_tag statement should be:
<%= form_tag search_qtl_table_index_path, :method => 'get' do %>

Related

Non standard rails routes in used in helper

In my routes.rb there is
resources :clients_assessments do
member do
get :medical_edit
get :mental_health_edit
get :personal_edit
end
collection do
end
end
Which gives routes
medical_edit_clients_assessment GET /clients_assessments/:id/medical_edit(.:format) {:action=>"medical_edit", :controller=>"clients_assessments"}
mental_health_edit_clients_assessment GET /clients_assessments/:id/mental_health_edit(.:format) {:action=>"mental_health_edit", :controller=>"clients_assessments"}
personal_edit_clients_assessment GET /clients_assessments/:id/personal_edit(.:format) {:action=>"personal_edit", :controller=>"clients_assessments"}
clients_assessments GET /clients_assessments(.:format) {:action=>"index", :controller=>"clients_assessments"}
POST /clients_assessments(.:format) {:action=>"create", :controller=>"clients_assessments"}
new_clients_assessment GET /clients_assessments/new(.:format) {:action=>"new", :controller=>"clients_assessments"}
edit_clients_assessment GET /clients_assessments/:id/edit(.:format) {:action=>"edit", :controller=>"clients_assessments"}
clients_assessment GET /clients_assessments/:id(.:format) {:action=>"show", :controller=>"clients_assessments"}
PUT /clients_assessments/:id(.:format) {:action=>"update", :controller=>"clients_assessments"}
DELETE /clients_assessments/:id(.:format) {:action=>"destroy", :controller=>"clients_assessments"}
When I try to use them in a helper file like
route = medical_edit_clients_assessment_path(id)
An error message is generated
No route matches {:action=>"medical_edit", :controller=>"clients_assessments"}
I have to change it to
route = "/clients_assessment/#{able_id}/medical_edit"
to get around the error message. What is funny though that a route like
route = (able_id.nil? ? new_client_path : edit_client_path(able_id))
works just fine.
I know someone one will ask for it so here is the entire routes.rb and the result of the rake routes
https://gist.github.com/3074287
Thanks, Russ
This might happened because id is nil

Rspec namespaced route specs failing even though route exists

I'm using rspec-rails (2.8.1) and rails 3.1.3.
I'm trying to test routes for my Admin::ZonesController. I have verified the route exists in both the browser and by running rake routes. I am not using ActiveRecord (if that matters). When I run the routing spec, it tells me:
ActionController::RoutingError: No route matches "/admin/zones/new"
Here is the test (spec/routing/admin/zones_routing_spec.rb):
require 'spec_helper'
describe "routing to zones" do
it "routes /admin/zones/new to admin/zones#new" do
{ :get => "/admin/zones/new" }.should route_to(
:controller => "admin/zones",
:action => "new"
)
end
end
Here is the controller action whose route I am trying to test (admin/zones#new):
class Admin::ZonesController < Admin::BaseController
before_filter :instantiate_variables
def new
#zone = Zone.new
#campaign = Campaign.new
#rules = [Rule.new]
end
end
Running rake routes gives me this:
POST /hooks/:resource(.:format) {:controller=>"hooks", :action=>"create"}
POST /services/:service/:method(.:format) {:controller=>"services", :action=>"create"}
admin_zones GET /admin/zones(.:format) {:action=>"index", :controller=>"admin/zones"}
POST /admin/zones(.:format) {:action=>"create", :controller=>"admin/zones"}
new_admin_zone GET /admin/zones/new(.:format) {:action=>"new", :controller=>"admin/zones"}
edit_admin_zone GET /admin/zones/:id/edit(.:format) {:action=>"edit", :controller=>"admin/zones"}
admin_zone GET /admin/zones/:id(.:format) {:action=>"show", :controller=>"admin/zones"}
PUT /admin/zones/:id(.:format) {:action=>"update", :controller=>"admin/zones"}
DELETE /admin/zones/:id(.:format) {:action=>"destroy", :controller=>"admin/zones"}
admin_widgets GET /admin/widgets(.:format) {:action=>"index", :controller=>"admin/widgets"}
POST /admin/widgets(.:format) {:action=>"create", :controller=>"admin/widgets"}
new_admin_widget GET /admin/widgets/new(.:format) {:action=>"new", :controller=>"admin/widgets"}
edit_admin_widget GET /admin/widgets/:id/edit(.:format) {:action=>"edit", :controller=>"admin/widgets"}
admin_widget GET /admin/widgets/:id(.:format) {:action=>"show", :controller=>"admin/widgets"}
PUT /admin/widgets/:id(.:format) {:action=>"update", :controller=>"admin/widgets"}
DELETE /admin/widgets/:id(.:format) {:action=>"destroy", :controller=>"admin/widgets"}
zones GET /zones(.:format) {:action=>"index", :controller=>"zones"}
POST /zones(.:format) {:action=>"create", :controller=>"zones"}
new_zone GET /zones/new(.:format) {:action=>"new", :controller=>"zones"}
edit_zone GET /zones/:id/edit(.:format) {:action=>"edit", :controller=>"zones"}
zone GET /zones/:id(.:format) {:action=>"show", :controller=>"zones"}
PUT /zones/:id(.:format) {:action=>"update", :controller=>"zones"}
DELETE /zones/:id(.:format) {:action=>"destroy", :controller=>"zones"}
root / {:controller=>"admin/zones", :action=>"new"}
My routes.rb looks like this:
D2CModularPlatform::Application.routes.draw do
post "/hooks/:resource" => "hooks#create"
post "/services/:service/:method" => "services#create"
namespace :admin do
resources :zones
resources :widgets
end
resources :zones
root :to => "admin/zones#new"
end
My controllers dir looks like this:
controllers
admin
base_controller
widgets_controller
zones_controller
application_controller
hooks_controller
services_controller
zones_controller
My spec/routing dir looks like this:
spec/routing
admin
zones_routing_spec
hooks_routing_spec
services_routing_spec
zones_routing_spec

Is there a way to fix a singular controller in rails 3.1?

I accidentally generated a singular controller comm_log without 's'. 's' was added to controller name, helpers, views, specs and routes. The customer_comm_log_path(f.customer_id, f.id) seems not right.
The relation is a comm log belongs to a customer and a customer has many comm logs.
resources :customers do
resources :comm_logs
end
The output of rake routes (related) is:
comm_logs_index GET /comm_logs/index(.:format) {:controller=>"comm_logs", :action=>"index"}
comm_logs_new GET /comm_logs/new(.:format) {:controller=>"comm_logs", :action=>"new"}
comm_logs_create GET /comm_logs/create(.:format) {:controller=>"comm_logs", :action=>"create"}
comm_logs_show GET /comm_logs/show(.:format) {:controller=>"comm_logs", :action=>"show"}
comm_logs_destroy GET /comm_logs/destroy(.:format) {:controller=>"comm_logs", :action=>"destroy"}
customer_comm_logs GET /customers/:customer_id/comm_logs(.:format) {:action=>"index", :controller=>"comm_logs"}
POST /customers/:customer_id/comm_logs(.:format) {:action=>"create", :controller=>"comm_logs"}
new_customer_comm_log GET /customers/:customer_id/comm_logs/new(.:format) {:action=>"new", :controller=>"comm_logs"}
edit_customer_comm_log GET /customers/:customer_id/comm_logs/:id/edit(.:format) {:action=>"edit", :controller=>"comm_logs"}
customer_comm_log GET /customers/:customer_id/comm_logs/:id(.:format) {:action=>"show", :controller=>"comm_logs"}
PUT /customers/:customer_id/comm_logs/:id(.:format) {:action=>"update", :controller=>"comm_logs"}
DELETE /customers/:customer_id/comm_logs/:id(.:format) {:action=>"destroy", :controller=>"comm_logs"}
Is there a way to fix the singular controller? Thanks.
Try script/destroy your_controller_name using the same controller name used to create it.

Problem with rails routing

I have the following in my routing config:
resources :users do
resources :apps, :controller => :user_apps
end
rake routes includes the following:
user_apps GET /users/:user_id/apps(.:format) {:action=>"index", :controller=>"user_apps"}
user_apps POST /users/:user_id/apps(.:format) {:action=>"create", :controller=>"user_apps"}
new_user_app GET /users/:user_id/apps/new(.:format) {:action=>"new", :controller=>"user_apps"}
edit_user_app GET /users/:user_id/apps/:id/edit(.:format) {:action=>"edit", :controller=>"user_apps"}
user_app GET /users/:user_id/apps/:id(.:format) {:action=>"show", :controller=>"user_apps"}
user_app PUT /users/:user_id/apps/:id(.:format) {:action=>"update", :controller=>"user_apps"}
user_app DELETE /users/:user_id/apps/:id(.:format) {:action=>"destroy", :controller=>"user_apps"}
However, when I try to access eg user_apps_path(1,2) I get /users/1/apps.2 rather than /users/1/apps/2.
Where am I going wrong?
I'm using rails 3.
The correct route is user_app_path(1,2) The pluralized version goes to the index action, making the second argument the format / extension of the request.

How to modify a link?

I need to modify this link to go to
channels/params[:channel_id]/messages
here is the current link
<%= link_to pluralize(#channel.messages.size, 'message') %>
result of rake routes
{:action=>"index", :controller=>"messages"} POST /channels/:channel_id/messages(.:format)
{:action=>"create", :controller=>"messages"}new_channel_message GET /channels/:channel_id/messages/new(.:format)
{:action=>"new", :controller=>"messages"} edit_channel_message GET /channels/:channel_id/messages/:id/edit(.:format)
{:action=>"edit", :controller=>"messages"}channel_message GET /channels/:channel_id/messages/:id(.:format)
{:action=>"show", :controller=>"messages"} PUT /channels/:channel_id/messages/:id(.:format)
{:action=>"update", :controller=>"messages"} DELETE /channels/:channel_id/messages/:id(.:format)
{:action=>"destroy", :controller=>"messages"} channels GET /channels(.:format)
{:action=>"index", :controller=>"channels"} POST /channels(.:format)
{:action=>"create", :controller=>"channels"} new_channel GET /channels/new(.:format)
{:action=>"new", :controller=>"channels"} edit_channel GET /channels/:id/edit(.:format)
{:action=>"edit", :controller=>"channels"}channel GET /channels/:id(.:format)
{:action=>"show", :controller=>"channels"}PUT /channels/:id(.:format)
{:action=>"update", :controller=>"channels"} DELETE /channels/:id(.:format)
If you would have used nested routes and if you redirect that link to index action in messages controller, then you can define your link like this:
<%= link_to pluralize(#channel.messages.size, 'message'), channel_messages_path(#channel) %>
You don't really need to "modify" it, since it most likely never worked anyway :)
Search for the path macro for your route, using:
rake routes
Then you can use the correct macro, which most likely is something like channel_message. You'll reach something like this:
<%= link_to pluralize(#channel.messages.size, 'message'), channel_message_path(#channel) %>

Resources