I'm wondering if i can generate a scaffold without getting that annyoing s after all scaffolds.
For example, when I run rails generate scaffold product, rails
generates a scaffold called products.
You can modify config/initializers/inflections.rb and provide custom Inflections for words that you want to pluralize/singularize differently. Something like:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "product"
end
would make it so rails g scaffold Product ... wouldn't pluralize the table name and controller name.
However, I would advise against doing this en masse, or just because you don't like pluralization. One of Rails strengths is "Convention over Configuration", and like in several cases, if you're not following the convention, it will be more trouble than it's worth in the long run.
May be you should think about changing your config/initializers/inflections.rb file. Learn more by links below:
Pluralizations and Singularizations (Inflections) in Rails 3
ActiveSupport::Inflector
Related
I'm trying to generate a scaffold named "slave". When I run the command rails generate scaffold slave rails changes some parts of the scaffold from slave to slafe. Affected, for instance, are the resource routes.
Why does this happen and how can I fix this problem?
I am using Rails 5.0.2 with JRuby on RubyMine.
This is happening because of an incorrect inflection. Rails inflections take a one-size-fits-all approach that works in most cases but can often catch you out because of how weird English pluralization is. In your case, the plural 'slaves' is being correctly generated from your input 'slave', but during the route helper generation rails evidently needs a singular version of 'slaves' and settles on 'slafe' because of this inflection rule:
inflect.singular(/([^f])ves$/i, '\1fe')
To fix this simply add a rule of your own:
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.singular('slaves', 'slave')
end
...run the generator again and it works:
$ rake routes | grep slave
slaves GET /slaves(.:format) slaves#index
POST /slaves(.:format) slaves#create
new_slave GET /slaves/new(.:format) slaves#new
edit_slave GET /slaves/:id/edit(.:format) slaves#edit
slave GET /slaves/:id(.:format) slaves#show
PATCH /slaves/:id(.:format) slaves#update
PUT /slaves/:id(.:format) slaves#update
DELETE /slaves/:id(.:format) slaves#destroy
Incidentally, the exact opposite problem can be caused by this rule:
inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
...which causes problems with e.g rails generate scaffold cafe - everything that should be 'cafes' becomes 'caves'. Argh!
As already said by #omnikron, you can try to force an "irregular" inflection at the file config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'slave', 'slaves'
end
I want to create a cafe and a cave controller.
When I try to create my cafe using rails scaffolding, via the command
rails g scaffold cafe name:string
It is deriving the plural form of "cafe" as "caves", which means I can't make my caves controller since the name is already used.
How can I make rails use the correct pluralisation?
You can create your own inflections.
Add this to your config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural "cafe", "cafes"
end
(Restart your server after making this change. This is not required for the scaffolding command itself but it will be required when you want to actually view/use the code)
Now when you run rails g scaffold cafe you'll get:
...
app/views/cafes
create app/views/cafes/index.html.erb
create app/views/cafes/edit.html.erb
create app/views/cafes/show.html.erb
create app/views/cafes/new.html.erb
create app/views/cafes/_form.html.erb
etc
This may help you: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-inflections
I'm having a problem with generating scaffold for a Regatta. When I run
rails g scaffold Regatta name:string start_date:datetime
I get a model named regattum and a controller called regatta_controller (instead of regattas_controller)
invoke active_record
create db/migrate/20110609221608_create_regatta.rb
create app/models/regattum.rb
invoke test_unit
create test/unit/regattum_test.rb
create test/fixtures/regatta.yml
route resources :regatta
invoke scaffold_controller
create app/controllers/regatta_controller.rb
invoke erb
create app/views/regatta
create app/views/regatta/index.html.erb
create app/views/regatta/edit.html.erb
create app/views/regatta/show.html.erb
create app/views/regatta/new.html.erb
create app/views/regatta/_form.html.erb
invoke test_unit
create test/functional/regatta_controller_test.rb
invoke helper
create app/helpers/regatta_helper.rb
invoke test_unit
create test/unit/helpers/regatta_helper_test.rb
invoke stylesheets
identical public/stylesheets/scaffold.css
Obviously this is an inflection problem, but whenever I modify /config/initializers/inflections.rb I get an error saying:
The name 'Regatta' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
I have tried everything I can think of to make it work, but I keep getting an error. Any advice on a solution or a workaround would be greatly appreciated!
Update
Here are some of the things I have tried:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'regatta', 'regattas'
end
That didn't change anything so I tried a few of the options below, among others,in varying combinations to no avail:
inflect.plural 'regatta', 'regattas'
inflect.singular 'regattas', 'regatta'
inflect.singular 'regatta', 'regatta'
Update 2
Here is the code I used in inflections.rb once I figured out what I was doing wrong:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'regatta', 'regattas'
inflect.singular 'regatta', 'regatta'
inflect.singular 'regattas', 'regatta'
end
Hopefully this will help someone out in the future!
I recognize this post is old, but I thought I would contribute my findings as well.
I have been integrating rpsec and cucumber into my application after much development (yes; naughty, naughty). I wanted to generate the rspec stubs for my models and get my application unit tested. I am usually able to move an associated migration out of db/migrate/ and run 'bundle exec rails g ' to regenerate the scaffolds and in this case, the rspec stubs. Not this time.
<terminal>
bundle exec rails g scaffold User email:string ...
updated_at:datetime roles_mask:integer --trace
invoke active_record
The name 'User' is either already used in your application
or reserved by Ruby on Rails. Please choose an alternative
and run this generator again.
My problem was resolved by temporarily commenting out the 'devise_for' line in my 'config/routes.rb' file
config/routes.rb
#devise_for :users
Bingo. And this isn't the only time that Devise has surprised me with 'black magic' that is not readily apparent. I un-commented the line once my rspec stubs were generated, of course. Now it's off to writing my unit tests!
Given the error message saying that Regatta is already in use in your application (it's obviously not reserved by Rails), I'm guessing the Regattum model is still in place.
I'd recommend destroying the scaffold and trying again. It sounds like, once a word is already defined in the application, Rails has a built-in protection against changing the inflection. So, this error being thrown is to protect against unexpected behavior in this very situation.
Here are the steps I did. In command line:
rails new lcdemo
rails generate scaffold Cove title:string
(created an entry in seeds.rb)
rake db:migrate
rake db:seed
In the controller and view files, instance variables are named cofe instead of cove.
The reason
"cove".pluralize
=> "coves"
"coves".singularize
=> "cofe"
The solution
You can edit your config/initializers/inflections.rb file to create a custom inflection to teach Rails that the singular of "coves" is "cove".
inflect.irregular 'cove', 'coves'
That'll be the Inflector working its magic. It looks like it's pluralizing "Cove" to "coves" in order to make the table name, and then singularizing "coves" (by analogy with "wolves", "knives" etc) to "cofe".
I don't know if it'll work, but you could try adding the following to config/initializers/inflections.rb before you run the scaffold generator?
inflect.plural "cove", "coves"
inflect.singular "coves", "cove"
I am running
ruby script/generate scaffold
or
ruby script/generate model
and I know the basic syntax, like
ruby script/generate scaffold Dude name:string face:boolean
but I do not know things like:
should names of variables have underscores or be camelCased?
what kind of variable types are acceptable?
Where can I find such information?
Type the command without arguments and the documentation is revealed:
$ script/generate model
You can use either camelcase or underscores for the model name.
Model names are singular; controller names are plural.
Field names use underscores.
I can't remember all the possible field types, I just look them up from the Migration docs, as linked above.
This document on Rails Migration would help.
With respect to the naming convention, I think the general adopted convention for Ruby on Rails is to have underscores.
To know which variable types are acceptable, refer to the section on Database Mapping.
There is a resource on the rails wiki as a List of Available Generators.
To check Rails naming conventions, topfunky's Pluralizer was useful.
there is a new syntaxis for Rails is rails generate