Scaffold only a subset of methods in controller - ruby-on-rails

Is there a way to generate a subset of methods in the controller using scaffold instead of the regular scaffold?
For example: Only create new and show?
I am using rails 3, ruby 1.9.2
Thanks

I do not think the default rails scaffold generator supports this in Rails 3.
However, this can be accomplished using the nifty-generators gem:
https://github.com/ryanb/nifty-generators

In plain Rails3, you can do this when you generate the controller:
rails generate controller Artists new create
So, you could just generate the model, and then the controller, and get the overall functionality you were looking for.

Related

there are some way to create a custom scaffold or generator on ruby on rails

I wish to create my custom generator, some like run the command "rails generator myScaffold entity field1:String field2:String
and the generator behavior like a normal scaffold, except it will not create the stylesshets, views, and the class of the controller would be customized.
Is it posible with rails? And is it a good and correct thing to do?
thanks!!!
All you need is available here http://guides.rubyonrails.org/generators.html

Rails create new MVC

I would like to create a model called models/thing.rb, a controller called controllers/things_controller.rb, and views called views/things/index.html.erb and views/things/hello.html.erb.
How do I do this from the rails console? I know I have to use rails generate.
Firstly generate the controller with two actions index and hello.
rails g controller things index hello
This command will generate the controller and the views with views/things/index.html.erb and views/things/hello.html.erb.
Then generate the model:
rails g model Thing attribute:type [...]
If you want to know more about rails generate command, you can take a look at A Guide to The Rails Command Line

Rails 3 generate scaffold for specific actions only

My setup: Rails 3.0.9, Ruby 1.9.2
I wish to generate a scaffold only for the create action, what's the syntax for that? I'm guessing it's something I append to
rails g scaffold Project name:string ...
I don't believe you can. I'm looking at the generator and I see nothing related to using an option to limit the template.
Generator
Template
Rails scaffolding is a very basic tool, it's not meant to be relied on, once you get a handle on the way rails works. However, you can use Ryan Bates' nifty-generator gem to generate scaffolds with greater control. Example:
rails g nifty:scaffold post name:string index new edit
Learn more here. https://github.com/ryanb/nifty-generators

Create scaffold by skipping the model

I have rails 2.3 and ruby 1.8.7
I want to create only a controller and view files for it. Is there any command in rails2.3 that will help me in doing this. I know this can be done in rails 3, but is there a way to do this in rails 2.3
Just say script/generate controller [controller_name] [action_name_1] [action_name_2] ... in the Rails directory to create a controller with the given actions and corresponding view files.

Generate a controller with all the RESTful functions

I am trying to generate a controller with all the RESTful actions stubbed. I had read at Wikibooks - Ruby on Rails that all I needed to do was to call the generator with the controller name and I would get just that. So, I ran script/generate rspec_controller Properties but got an empty controller.
Any other suggestions would be greatly appreciated.
I don't know about an automated way of doing it, but if you do:
script/generate controller your_model_name_in_plural new create update edit destroy index show
All of them will be created for you
Update for Rails 4
rails g scaffold_controller Property
In Rails 3 there is also rails generate scaffold_controller .... More info here.
EDIT(due to some comments) : Original question was in 2010 - hence the answer is NOT for RAILS 4 , but for rails 2!!
try using scaffolding.
script/generate scaffold controller Properties
Section of Official docs on Ruby On Rails
I'm sure you can find more info if you do a google search on rails scaffolding. Hope that helps.
EDIT:
For RAILS 4
rails g scaffold_controller Property
In Rails 4/5 the following command does the trick for me.
rails g scaffold_controller Property --skip-template-engine
It generated the controller actions but not the view.
Rails 5.1
Starting point:
You have created a model without a controller, nor views (eg thru: rails generate model category)
Objective:
Upgrade it to a full RESTful resource
Command:
rails generate scaffold_controller category
It stubs out a scaffolded controller, its seven RESTful actions and related views. (Note: You can either pass the model name CamelCased or under_scored.)
Output:
varus#septimusSrv16DEV4:~/railsapps/dblirish$ rails generate scaffold_controller category
Running via Spring preloader in process 45681
create app/controllers/categories_controller.rb
invoke erb
create app/views/categories
create app/views/categories/index.html.erb
create app/views/categories/edit.html.erb
create app/views/categories/show.html.erb
create app/views/categories/new.html.erb
create app/views/categories/_form.html.erb
invoke test_unit
create test/controllers/categories_controller_test.rb
invoke helper
create app/helpers/categories_helper.rb
invoke test_unit
invoke jbuilder
create app/views/categories/index.json.jbuilder
create app/views/categories/show.json.jbuilder
create app/views/categories/_category.json.jbuilder
You're looking for scaffolding.
Try:
script/generate scaffold Property
This will give you a controller, a model, a migration and related tests. You can skip the migration with the option --skip-migration. If you don't want the others, you'll have to delete them yourself. Don't worry about overwriting existing files, that won't happen unless you use --force.
As klew points out in the comments, this also defines the method bodies for you, not just the names. It is very helpful to use as a starting point for your REST controller.
In Rails 4 it's rails g controller apps new create update edit destroy show index
Or rails generate controller apps new create update edit destroy show index if you want to write out the full term :).
script/generate rspec_scaffold Property
There's no way (that I know of? that is documented?) to stub out a controller except through scaffolding. But you could do:
script/generate controller WhateverController new create edit update destroy show
One solution is to create a script that accepts one parameter, the controller name, and let the script type the whole command for you.
Create a new file, say, railsgcontroller
Make it executable and save it on path
Run it like:
$ railsgcontroller Articles
die () {
echo "Please supply new rails controller name to generate."
echo >&2 "$#"
exit 1
}
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
rails g controller "$1" new create update edit destroy show index

Resources