Rails Routes: Wrong Singular for Resources

Rails routes: Wrong singular for resources

Rails guesses. It's not perfect. In config/initializers/inflections.rb add

ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'request_cache', 'request_caches'
end

You'll need to restart the server as it's in an initializer.

Rails routing. Singular resource

Defining a singular resource in your routes will not generate a route to an index action by design. The singular resource implies you're always going to lookup this resource without specifying an ID and consequently a get to index for a singular resource just doesn't make logical sense. So, a GET to your url "/user" will route to a show action for that singular resource and not an index.

EDIT: Since your issue isn't obvious, I'd simplify your routes until you can at least hit the controller you'd expect and then build from there.

config/routes.rb

 scope :module=>"frontend" do
resource :user
end
#ensure you don't have any other user routes listed before this that would match "/user".

app/controllers/frontend/users_controller.rb

 module Frontend
class UsersController < ApplicationController
def show
raise "in frontend/show"
end
end
end

Rails Routing from a plural to singular resource

Please try this

= link_to edit_admin_customer_content_setting_path(customer_id: @owner.id, id: @content.id)

When you take a look at the output of the rake routes, you can see that in the third column you are being told what parameters it expects:

edit_admin_customer_content_setting  GET     /admin/customers/:customer_id/content_setting/:id/edit(.:format)     admin/content_setting#edit

In your case: customer_id and id


UPDATE

If you would like to avoid passing two ids to your helpers, you can use shallow routes. Please take a look at the documentation here (scroll little bit down to Shallow Nesting)

Correct route for a singular nested resource

In your routes, define the child resource using the singular resource method:

resources :parent_resources do
resource :child_resource
end

By convention, the controller for the child will still be ChildResourcesController, plural.

Rails has a pretty good guide to routing. See the section on singular resources.

Rails routing problem. Changing resource from plural to singular give No route matches error

This question is about a bug in Rails:

https://rails.lighthouseapp.com/projects/8994/tickets/267

Rails 3 route has incorrect singular form

Most of the time, rails knows about the correct pluralizations of the words, e.g: model name.
However, In other cases, You can define correct conversions in rails using Inflections.

Rails Route Issue - Plural or Singular?

map.resources :dogs # => blows up map.resources :dog # is ok, but... dogs_path # => blows up dog_path # => ok

Based upon input I found in another thread, this made me realize I would need a plural controller.

Re-creating the controller as plural (advertisementS) resolved issue.

rails link path and routing error when model singular and plural name are the same (e.g. equipment, species)

This issue has cropped up before and is related to how rails scaffolding generates the new.html.erb file for models that have names like 'equipment' which are both singular and plural.

If you inspect the form_for in the new.html.erb file you'll see equipment_path in the link_to at the bottom. For these models with singular==plural names that refers to a route that is actually for the show action, hence your error message.

The advice is often along the lines of 'avoid model names like this if you can' or it involves a bit of messing around with the config/initializers/inflections.rb file to force a plural version of the model name. Of course then you end up with an app with very odd sounding references to models: 'equipments' isn't very nice to work with (and someone later on will 'fix' it, messing things up again).

To keep the model name grammatically correct, you need to fix the form_for i.e.:

<% form_for(@equipment, :url=> {:action=>'create'}) do |f| %>

and the link_to:

<%= link_to 'Back', equipment_index_path %>

How do I create a singular GET path for a resource including helpers?

Just place your singular routes get statement before the resources statement,

get '/splash', to: 'splashes#index'
resources :splashes, only: [:index, :create, :destroy]

Now, the rake routs will give your the below result,

splash   GET    /splash(.:format)                                splashes#index
splashes GET /splashes(.:format) splashes#index
POST /splashes(.:format) splashes#create
DELETE /splashes/:id(.:format) splashes#destroy

By that above approach the delete and get routes get overrided based on the defintion sequence, because they both has the same named helpers spash_path. So, I suggest you to add named helper to custom get /spash routes like below,

resources :splashes, only: [:index, :create, :destroy]
get '/splash', to: 'splashes#index', as: 'splash_index'

So, now you will get a separate named routes splash_index for your custom get route. There's a another solution also,

resources :splashes, only: [:index, :create, :destroy]
resources :splash, only: [:index], controller: :splashes

So, that you will get a different named helper for your /spash routes,

splashes GET    /splashes(.:format)                           splashes#index
POST /splashes(.:format) splashes#create
splash DELETE /splashes/:id(.:format) splashes#destroy
splash_index GET /splash(.:format) splashes#index


Related Topics



Leave a reply



Submit