Rails: What is the convention for controllers with many words - ruby-on-rails

Say I have the model CourseGroup. What would be the controller's name?

The controller name would be course_groups_controller.
http://itsignals.cascadia.com.au/?p=7
To find the name for any model, you can open up a rails console and do "ModelName".tableize. Then just add "_controller" to the end. This would result in model_names_controller.

Here's an easy way to find out the naming conventions: Just create a throw-away Rails app in a temp directory, with a scaffolded model:
rails blog
cd blog
./script/generate scaffold post subject:string content:text
You can then browse through the files and directories to see how things are named. I like to keep one of these around just to refer to from time to time. And by the way, running the generators without any parameters gives help output which includes examples of naming conventions:
./script/generate scaffold

Related

Listing the different scaffolds generated in your rails application

I have generated two scaffold using the following two commands:
$:- rails generate scaffold User user:string gender:string
$:- rails generate scaffold Microposts microposts:string
Is there any command that will list me the different scaffolds i have generated so far?
It should give me the output similar to this:
Scaffold generated:
User
Microposts
Not that I know of, but you can see the direct result in your application, plus after each scaffold you see which files are generated. To quote RoR guides:
A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
A scaffold in itself is not one thing, it's a collection. What would be the point of only displaying the name you used to scaffold all these files?
I don't think so - scaffolding generates models, views and controllers for a given class (in your case User and Microposts) - if you look at app/models you'll see User.rb and Microposts.rb. So maybe that's nearly the same thing?

Access Migrations via console in Ruby on Rails

I am new to Ruby on Rails . This might be a very foolish question.
I have created a migration using
rails generate migration Kapol name:string position:integer
rake db:migrate
Then using phpmyadmin i copied the database already present
I then opened up rails console
My question is can i use the method Kapol.find(1)??
because when i tried it using singular or plural it says
unitialized constant:Kapol
I know that there has to be a method but where to specify it?
You must generate a model in case to create a table for it, because the migration is usually used to modify existing tables.
It might be confusing that the model generator also creates migration file in your migrations folder. The only difference is that the model generator also generates initial code to create table, on the other hand, the migration generator creates only migration file without initial code.
rails generate model Kapol name:string position:integer
More information: http://guides.rubyonrails.org/getting_started.html#generating-a-model
If you're very new to Ruby on Rails, probably the best thing for you to do is create a scaffold, which gives you your migration file, your model file, your controller file, and various view files, test files, etc. etc., which all work well together. Then you can play with these and build up from there.
rails generate scaffold Kapol name:string position:integer
If you're happy with the migration that was automatically generated, then rake db:migrate and you're all set.
As Andrew says below, you can also just generate any of those files one at a time by replacing 'scaffold' with 'model', etc.
Your Kapol.find(1) is correct.

rails g scaffold series name:string - is this a naming convention error or something else

On my blog I'm have posts that belong to a series. I've tried to scaffold series but there are some problems with routes.
The pluralization engine doesn't get it right so I had to manually change Sery, #series, and #sery which is not a big deal.
The routing seems to be ok with resources :series. But then when I try to create a series the form_for helper complains about the route.
And then when I create it with console it works but rails is still complaining about routes.
Please create a simple app and see what the problem is.
rails new test_series_app
And then run the scaffold generator:
rails g scaffold series name:string
And see how the routes are getting mixed up and help me out please!
For the record, I put the singularize code into the scaffold generator (yes, my one contribution to Rails). All it does is check if record_name == record_name.pluralize. If it does and you haven't passed in --force-plural, it calls record_name = record_name.singularize.
In this case "series".pluralize is the same as "series".singularize so I assume it won't do anything.
So if you're having problems w/ it, you need to write an inflector for the word.
(I wrote it after Jeremy Kemper's 2008 RailsConf keynote in which he accidentally passed in a plural model name causing himself all sorts of grief in the middle of his talk.)

How does these rails generate command differ? and what basically rails generate means?

rails generate migration
rails generate model
rails generate scaffold
rails generate controller etc.
How these differ?
According to rails guides:
Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend time writing. That’s what we have computers for.
rails generate commands family used to provide simple and easy way for developer to create different object types.
rails generate migration - creates DB migration script in db/migrations directory so developer can setup his DB.
rails generate model - creates model class with associated migration, test and fixtures (test data).
rails generate scaffold - creates all nedded classes with basic logic and presentaion. It creates controller (with simple CRUD logic), model, fixtures, functional and unit tests.
rails generate controller - creates controller with associated functional tests, helper and basic views templates.
You can read more here: http://guides.rubyonrails.org/command_line.html#rails-generate
They differ in the sense that they generate different stuff.
migration will generate a database migration file,
model will generate a model(with a migration and a spec by default)
scaffold will generate a scaffold of a resource
and controller will generate a controller.
generate means it will create the files for you with boiler plate code already in place(you will still need to edit them though..but scaffold can get you working with a basic application already)
Read more about it here: http://guides.rubyonrails.org/command_line.html#rails-generate
rails generate is a command line script for quickly generating the code for various Rails' constructs.
In the example you give they differ by what they produce, with the first argument being the type of code generated. For example if I wanted to create a User model I would run:
`rails generate model user`
The model file, test file and migration would be created for me.
You should read the Rails' documentation to find more.
**rails generate model user:
The above command create a Template Object that is a mirror image of the database table.
For example, if you have a database table that is named users that has a name:string, and email:string field,then "rails generate model user" create an Object that mirrors that user table with a few addition.
Here are the similarity they both have name:string,email:string the both have the word user.
The difference are few but significant: user is Capitalized in the model name like "User".
The Table add create_by and updated_by automatically.
migration:db create the database mirror using the model as a model.RECURSION ANYONE?

Creating Rails Generator For Model/View/Controller/Mailer

I have an app that's heavily form-based. Many of the forms are constructed exactly alike so it seems like a natural fit for a generator. What I want to do is create one that works like this (fictitious example):
rails g request_form name:string phone:string date_of_birth:date
In any case, the standard, empty controllers, helpers, models, and so on won't quite do. I've read the Rails code but it's, frankly, not been a heck of a lot of help in puzzling this out. What I want to do specifically is:
Create a model and migration given the fields specified on the command line
Create a controller and helper, based on my template
Create views based on my templates
Create empty specs
Create a mailer based on my template
Create mailer views based on my templates
I'm getting stalled at square 1: How the heck do I get the ARGV part of the rails g command -- that is, the field names? Then there's square 2: How do I hook into the built-in generators, where appropriate and fill in my own stuff where not?
This is analogous to
rails g scaffold blah:type blah1:type
so I don't think this is biting off more than I can chew...
Any help mucho appreciated!
All inspiration needed in this great gem: https://github.com/ryanb/nifty-generators
The awesome Ryan Bates has a screencast on writing generators in Rails 3, have you watched that?

Resources