rails script/generate scaffold problem - ruby-on-rails

I'm new to rails and was trying out the scaffold command - the following scaffold runs and works when I view it via web brick
script/generate scaffold book title:string
the following fails - gives me a weird route error
script/generate scaffold application name:string
the following works
script/generate scaffold app name:string
can anyone shed some light on this? Is 'application' a reserved word?

All your controllers are subclasses of ApplicationController, created by Rails. you can't create another controller with this name.

Yes Application is a reserved word
You can see a complete list on the Wiki
http://wiki.rubyonrails.org/rails/pages/reservedwords

Just so you know, make sure not to do this:
script/generate book title:string
You need to have the word scaffold after generate, like this:
script/generate scaffold book title:string

Related

rails g scaffold articles isn't executed

I am trying to generate a scaffold in an existing project but when I run the command rails g scaffold articles title:string date:string content:text image:string nothing happens...
$ rails g scaffold articles title:string date:string content:text image:string
I can't ever generate a controller???
rails g controller articles do nothing....
If I try in a new project it works what could be the problem please ?
I had to run stop spring and everything is back to normal !

Issue with scaffold in Ruby on Rails

I'm trying out a few things in Rails and I got a particular issue with the scaffolding process. It might be something I'm not familiar with, let me show you what I did:
First:
rails g controller painel painel
to create a simple front page
Second:
rails g scaffold Noticia title:string category:string tags:string text:text
Then I repeated the same line but changing the name like so:
rails g scaffold Artigo title:string category:string tags:string text:text
and I did the same with Ideia and Analise.
Done that I looked at the files structure and the controllers for Noticia and Ideia were noticia_controller.rb and ideia_controller.rb but the other two were artigos_controller.rb and analises_controller.rb , two were plural and two singular. Naturaly the first two didn't work out on the page as they should and I tried to repeat the same process again and again just to get the same result. I'd appreciate any ideas on the matter, thanks!
This is due to the pluralization rules in Rails being better suited for English than for other languages.
I would strongly recommend avoiding the scaffold generator and just generating your controllers and models using these two commands:
rails g controller artigos
rails g model artigo category:string
And then building out from there.

Rails: How to run `rails generate scaffold` when the model already exists?

I'm new to Rails so my current project is in a weird state.
One of the first things I generated was a "Movie" model. I then started defining it in more detail, added a few methods, etc.
I now realize I should have generated it with rails generate scaffold to hook up things like the routing, views, controller, etc.
I tried to generate the scaffolding but I got an error saying a migration file with the same name already exists.
What's the best way for me to create scaffolding for my "Movie" now? (using rails 3)
TL;DR: rails g scaffold_controller <name>
Even though you already have a model, you can still generate the necessary controller and migration files by using the rails generate option. If you run rails generate -h you can see all of the options available to you.
Rails:
controller
generator
helper
integration_test
mailer
migration
model
observer
performance_test
plugin
resource
scaffold
scaffold_controller
session_migration
stylesheets
If you'd like to generate a controller scaffold for your model, see scaffold_controller. Just for clarity, here's the description on that:
Stubs out a scaffolded controller and its views. Pass the model name,
either CamelCased or under_scored, and a list of views as arguments.
The controller name is retrieved as a pluralized version of the model
name.
To create a controller within a module, specify the model name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes helper,
template engine and test framework generators.
To create your resource, you'd use the resource generator, and to create a migration, you can also see the migration generator (see, there's a pattern to all of this madness). These provide options to create the missing files to build a resource. Alternatively you can just run rails generate scaffold with the --skip option to skip any files which exist :)
I recommend spending some time looking at the options inside of the generators. They're something I don't feel are documented extremely well in books and such, but they're very handy.
Great answer by Lee Jarvis, this is just the command e.g; we already have an existing model called User:
rails g scaffold_controller User
For the ones starting a rails app with existing database there is a cool gem called schema_to_scaffold to generate a scaffold script.
it outputs:
rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string
from your schema.rb our your renamed schema.rb. Check it
In Rails 5, you can still run
$rails generate scaffold movie --skip
to create all the missing scaffold files or
rails generate scaffold_controller Movie
to create the controller and view only.
For a better explanation check out rails scaffold
This command should do the trick:
$ rails g scaffold movie --skip
You can make use of scaffold_controller and remember to pass the attributes of the model, or scaffold will be generated without the attributes.
rails g scaffold_controller User name email
# or
rails g scaffold_controller User name:string email:string
This command will generate following files:
create app/controllers/users_controller.rb
invoke haml
create app/views/users
create app/views/users/index.html.haml
create app/views/users/edit.html.haml
create app/views/users/show.html.haml
create app/views/users/new.html.haml
create app/views/users/_form.html.haml
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
invoke jbuilder
create app/views/users/index.json.jbuilder
create app/views/users/show.json.jbuilder
I had this challenge when working on a Rails 6 API application in Ubuntu 20.04.
I had already existing models, and I needed to generate corresponding controllers for the models and also add their allowed attributes in the controller params.
Here's how I did it:
I used the rails generate scaffold_controller to get it done.
I simply ran the following commands:
rails generate scaffold_controller School name:string logo:json motto:text address:text
rails generate scaffold_controller Program name:string logo:json school:references
This generated the corresponding controllers for the models and also added their allowed attributes in the controller params, including the foreign key attributes.
create app/controllers/schools_controller.rb
invoke test_unit
create test/controllers/schools_controller_test.rb
create app/controllers/programs_controller.rb
invoke test_unit
create test/controllers/programs_controller_test.rb
That's all.
I hope this helps

Can generate scaffold put the controller in a namespace?

I want to generate the scaffold in a Rails app, generating the model as usual but having the controller inside the admin namespace. Is it possible?
The first time I've done it, I run
script/generate scaffold blog
and then refactored the controller, views, etc. My prefered solution at the moment is:
script/generate scaffold admin::blog
and then refactor the model, unit test and migration; it's less work.
If there's a better answer, I'll accept it.
You can do this for rails < 3:
script/generate scaffold Blog title:string
or
script/generate scaffold admin::blog title:string
For rails > 3:
rails g scaffold Blog title:string
or
rails g scaffold admin/blog title:string
This question is pretty widely asked on stackoverflow. And I also faced this problem and found no standard solution for that.
So, I created rails-admin-scaffold gem (for now it's Rails 4 only) which automates this process and wrote an article with more detailed explanation. Hope it would be helpful for someone.
For Rails 6:
assuming you have a model like:
rails g model Foo name:string
then you can do this with
rails g scaffold_controller Admin/Foo name:string --model-name="Foo"
(specifying the model name stops the controller from referring to the model Admin::Foo which would be the default)
NB: this isn't perfect; You'll have to fix up a bunch of path errors in your views and tests - but it gets you 80% of the way there.

Is there an option to generate scaffolding without generating migrations?

One of the things that bothers me in Rails is that scaffolding generates migrations that I don't always need.
Is there a way to generate the scaffold without the corresponding migration?
Use the --skip-migration parameter. For example:
rails generate scaffold post title:string body:text --skip-migration

Resources