How to customize the singularization of class name in rails - ruby-on-rails

I am working on Rails 4.1.0. And i have generated leaves_controller and Leave model in my application.
But the application generated routes for leaves as like new_leafe, edit_leaf etc.
Actually I want the singularize string of Leave as Leave only, like new_leave_path, edit_leave_path.
If any idea to singularize class name in Rails, please share.

If it's only the routes you wish to change, you could use the as: option:
#config/routes.rb
resources :leaves, as: "leave"
--
Alternatively, if you'd like to set the term within Rails, you may wish to use an Inflector like this:
How do I override rails naming conventions?
#config/initializers/inflectors.rb
# Add new inflection rules using the following format
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'leave', 'leaves'
end

Related

Why Controller names are plural but application_controller is itself a singular

Why Controller names are plural but application_controller is itself a singular.
I was just curious when i scaffold a person and rails generated a people_controller for it, I was impressed but then I saw application_controller and it is singular. Does it has an explanation or it is as it is ?
Rails is full of conventions and plural names for controllers is simply one of those conventions. It is so Rails can locate the appropriate controller when you put resources :books in your routes.rb
You can override the convention and specify the controller explicitly, resources :books, controller: "library". In this case, Rails will look for LibraryController.
With ApplicationController, you don't call it directly and Rails doesn't need to do this lookup and so it can be named whatever. You can rename it, for example.

Rails routing for Quiz model

So i've looked around and don't see a built in way to handle words that are named weirdly in the english language.
I have a Quiz model, which means that i have an index action of \quizes.
resources 'quizes', only: ['index', 'new', 'show']
I would like to be able to do things like
quizes_path #Note this one works
quiz_path(:id) #this does not
I have to do quize_path(:id)
Here is the rake routes, notice that the path names are quize instead of quiz.
quizes GET /quizes(.:format) quizes#index
new_quize GET /quizes/new(.:format) quizes#new
quize GET /quizes/:id(.:format) quizes#show
This is more for learning. I know i could just manually specify the singular resources and be done.
The ultimate question, is there a way to specify this in resources so that it knows that the singluar resources should remove 'es' instead of just 's'
EDIT
Here is the correct inflector based on the resources. Thanks!
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.singular /^(quiz)es/i, '\1'
end
Use quizzes instead of quizes in your route definition:
resources :quizzes
Then the singular routes are:
new_quiz GET /quizzes/new(.:format) quizzes#new
edit_quiz GET /quizzes/:id/edit(.:format) quizzes#edit
quiz GET /quizzes/:id(.:format) quizzes#show
As #Anand's answer says you need to spell "quizzes" correctly in this instance. To answer your general question you're looking for inflections in the file config/initializers/inflections.rb. This has been asked before: Ruby on Rails Inflection Issue and Ruby on Rails: How do you explicitly define plural names and singular names in Rails? as examples.

undefined local variable or method `new_media_path' - resources to resource

I have odd problem:
After starting server I got this error:
undefined local variable or method `new_media_path'
To repair this i must go to routes.rb and change
resources :media
to
resource :media
and again to
resources :media
It's annoying. Any ideas to solve this?
You should try new_medium_path because media is plural form of medium
If you run rake routes you will see all available routes.
You can also inform rails about the proper pluralization using the Inflector class. It handles most works fine, but non-standard pluralizations like 'media' aren't always pre-defined. To add your own, edit config/initializers/inflections.rb, and add this at the end:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'medium', 'media'
end
This should let Rails handle all the plural/singular stuff - note this will affect that it thinks DB table names will be as well, so it'll expect the model to be class Medium, and the table name will be media
To turn the plural and singular to the same thing (i.e. always 'media'), use:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable 'media'
end

Retrieve the controller name from a class name following RoR conventions

I am using Ruby on Rails 3.0.9 and I would like to build a controller name from a class name as well as possible following RoR naming conventions. For example, if I have the Articles::Comment class I would like to retrieve the articles/comments string.
Maybe it exists a RoR method created by developers to handle internally these conventions, but I don't know that.
How can I retrieve the controller name as in the example above?
You're looking for the underscore method. More info here.
"Articles::Comment".underscore
Or, if you've got the controller class itself, it would be like this:
Articles::Comment.name.underscore
EDIT
As of routes, they are built one piece at a time, even when you namespace them. When you do something like this:
map.resources :articles do |articles|
articles.resources :comments
end
What rails is going to do is, first:
"articles". classify # which yields "Article" then rails is going to append "Controller" to it
Then it's going to get "comments" and do the same, but this one is going to be routed under "/articles". Rails does not namespace internal resources, so, the controller has to be CommentsController, not Articles::CommentsController.
Only then you clearly namespace something, Rails is going to namespace your classes:
map.namespace :admin do |admin|
admin.resources :articles # will require controller "Admin::ArticlesController"
end
I hope it's clearer now.

Ruby on rails to_param with multiple fields for SEO

I am trying to make my urls prettier and still use restful resources. I understand that you can override the to_param method if you object has a name property like this:
def to_param
self.name
end
which will give you the route /:model/:name. This is all straightforward, but I have to be capable of having the same name with multiple different languages. I haven't been able to find a blog entry on how to do this, so how can i override the to_param method to provide me a route similar to /:model/:language/:name ?
You could always do:
/language/:language/model/:name
You'd do this with nested routes:
map.resources :languages do |l|
l.resources :profiles
end
Then your route would be:
langauge_profile_url('spanish', #profile)
However...
Depending on what you're trying to do you might be better of using the built in rails i18n stuff. Is this so users can browse the site in different languages??

Resources