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
Related
I have a model named Fave. It belongs to another model named User.
I'm trying to find a User's Faves by calling #user.faves. My server gives me back the following error:
NameError: uninitialized constant User::Fafe
Why would it think the singular of "faves" is "fafe"? Is there some other plural form I can use that will point to "fave"?
You can pass the class name while setting up the association
has_many :faves, class_name: "Fave"
Can we try this in config/initializers/inflections.rb? This could work
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'fave', 'faves' #append this to the existing ones
end
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.
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
I have a CRUD resource defined in my routes.rb file: resource :user.
I'm adding a new controller method for the user called search_places, which is performed on the user to find other users with the same places. I'm adding a route it.
Right now, I have:
post '/user/search_place', which isn't very DRY. I'm new to Rails and I was reading the Rails routing documentation and figured that I could possibly use
resource :user do
collection do
post 'search_place'
end
end
Is this considered good practice? I know this works (it passes my rspec route test), but is that how its best done?
Thank you,
When you add second don't need of first.
Add this:
resources :user do
collection do
post 'search_place'
end
end
Remove this:
resources :user
That makes DRY :)
Suggestion: Resources name should be defined in plural if u follow rails convention. (i.e) resources :users
I am using Ruby on Rails to create a website for a game I play.
I have a User model and a Starbase model. The relationship I am trying to setup is like so
class User < ActiveRecord::Base
has_many :starbases
end
class Starbase < ActiveRecord::Base
belongs_to :user
end
However when I open script/console and try to access the users starbases it gives me an error: NameError: uninitialized constant User::Starbasis.
It seems as if it is a problem with inflection and rails is not pluralizing starbase correct.
I have tried adding this to the inflections.rb in the intializers folder:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'starbase', 'starbases'
end
but it still does not solve the issue. Could anyone give advice on how to get this working?
Have you tried adding a line for the inverse inflection (i.e. 'singular'):
inflect.singular "starbases", "starbase"
I tried your example in my console and it was the singularization that caused problems, not the other way around. I'm not sure if this fixes other issues (like routes), but it should fix the simple stuff (I think).
Little trick i picked up to double check how Active Support might singularize, or pluralize my Class names, and/or Module names.
have your rails app server running and in a new tab enter into your rails console by typing rails console. In there you can easily double check for the correct style for your names.
long way ActiveSupport::Inflector.pluralize "fish"
# => "fish"
short way "fish".pluralize
# => "fish"
You can find more examples here
https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb