Inconsistent Routing name generation in Rails default routes - ruby-on-rails

I have a model Leave and a controller name 'LeavesController'. When I set resources :leave[singular] to the routes.rb then the routes name are ok. But when I set the resources :leaves[plural], then I get some funny routing name. See screenshot for details. [leafe, new_leafe, edit_leafe]
How can I get rid of these funny routing name?

In config/initializers/inflections.rb uncomment/add the following
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'leave', 'leaves'
end
Explanation:
Rails uses Inflectors to singularize/pluralize words automatically. This is how things like singular model names are transformed into their plural table names. Rails is typically able to guess the correct pluralization/singularization but can sometimes get confused. The inflections.rb initializer allows you to explicitly define how you want to handle singularization/pluralization when Rails doesn't give you what you are looking for.

Related

Rails model naming partial acronym word

So. For some reason I'm struggling with Rails naming today. I feel like the best naming for a model I'm creating is DNSRecord for the camel case class model name and dns_record for snake case referencing -- Rails wants to name it DnsRecord.
I've seen a solution if the entire word is capitalized for example: API
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'API'
end
but doesn't seem to work well as:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'DNSRecord'
end
it still wants to refer to DnsRecord. I know this is a small thing but DNSRecord feels better to me.
I believe you will want inflect.acronym 'DNS' rather than 'DNSRecord'. You will likely also have to restart your server after updating the inflections file.

How to get accurate pluralization with Rails pluralization

I need very accurate pluralization for my application
I am currently using the Rails built in pluralization method.
I found a problem with the word 'foot'.
When I do:
"foot".pluralize(2)
=> "foots"
The correct pluralization should be feet.
How can I get more accurate results?
I have looked for other gems and they all seem to make the same error.
you need to define below code inside your config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'foot', 'feet'
end
there are some of plural are not define yet, so you need to define it inside inflections.

Insert routes programmatically in Rails

I'm creating an engine that needs to insert some routes into the application's router. For this particular gem, I'd rather not application's routes.rb if possible. Is there a way to insert routes at a particular location in the router via code? I'm looking for an API that does something like:
Rails.application.routes.insert("resources :foos", :before => "some string in routes.rb")
If I create a config/routes.rb inside the engine and define some routes, this kind of works. Rails is smart enough to mix the engine's routes into the application's routes, but it tacks them on at the end of the route list. I need them to appear at the beginning so the engine's routes take priority.
I'm aware that I can namespace the routes by mounting the engine in the application's routes.rb, but this creates a routing structure that I don't really want. I want the engine's routes to look they are a part of the application by defining some routes in the actual application.
I have a workaround which is to add the following to the application's routes.rb.
Rails.application.routes.draw do
MyEngine.setup_routes(self)
#...other routes below
end
MyEngine.setup_routes looks like
def self.setup_routes(map)
map.get 'a_path', :to => 'a_controller#a_path'
end
This at least allows me to control the point where the routes get defined in the application's route list, but the user still has to manually update his routes.rb (or I have to build an installer that does it). It seems like there should be a way to tell rails to tack some routes onto the start of the route list...

Ruby on Rails: How do you explicitly define plural names and singular names in Rails?

For example, I'm using "Bonus" as my model, so I'd expect "bonuses" to be the plural form and "bonus" to be the singular form.
However, in Ruby, this results in:
"bonus".pluralize # bonus
"bonuses".singularize # bonuse
So, when I do a "has_many :bonuses", for example, it doesn't use the Bonus.rb model (since Ruby expects a Bonuse.rb model instead). Is there a way to correct that in Ruby on Rails somehow such that "bonuses" acts as the plural form for the model bonus.rb?
In config/initializers, you will find a file called inflections.rb. There are some instructions in here, but you will want something along the lines of:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'bonus', 'bonuses'
end
Just to back up bcarlso, more on Inflector can be found here:
http://4loc.wordpress.com/2009/04/09/inflector-rails-pluralization/
Note that the position of the Inflector.inflections block is important and, as noted in the link reference, must be after the Initializer.run block.
I believe you use the Inflector in your environment.rb (memory's a bit sketchy though)
If I remember correctly you put it in a block
Inflector.inflections { | i | i.irregular 'bonus', 'bonuses' }

Ruby on rails path helpers

I know this is a minor issue, but why, if you use scaffolding in RoR, can you use lines like 'new_model name here_path' in link tags, but without using scaffolding, I get a NameError? For example, I have a simple address book app that uses basic CRUD operations. I am a RoR beginner but wanted to build an app without scaffolding and these kind of things don't seem to work. I compared my config/routes.rb and app/helpers/* with those in a scaffolded app and they are no different. What am I missing?
One way to check your routes and paths is to run:
rake routes
It outputs all your routes and paths.
Scaffolding sets up resource routes in the routes.rb file. The resource routes are what give you the path and url helpers. When you don't use scaffolding the routes aren't added, you must do it by hand.
Resource Routes can be added like so:
map.resources :models
where :models is the plural name of one of your models.

Resources