What is the difference between default scaffold and nifty:scaffold? - ruby-on-rails

I am trying to generate a layout which I can use between my rails webapp and mobile versions. I have been using nifty-generator, but it says the generated files are identical to what was generated by rails3 new application creation.
What's the major difference between default scaffold and nifty:scaffold?

If you visit the Github page (https://github.com/ryanb/nifty-generators) for the project under 'Troubleshooting and FAQs' it answers a few questions including this one. The response given there is:
One of the primary differences is that nifty:scaffold allows you to
choose which controller actions to generate.
rails g nifty:scaffold post name:string index new edit
There are a few changes to the generated code as well, such as no XML
format by default.
It also offers support for HAML, Shoulda, and RSpec.
Once you get a handle on the code that Rails needs in its RESTful controllers I would highly recommend using Inherited Resources (https://github.com/josevalim/inherited_resources) instead. It really helps DRY up your controllers.

Related

Rails hooking a custom generator into scaffold generator

In a new Rails 6.1.3 project, I'm attempting to have bin/rails generate scaffold Thing name:string invoke the typical Rails scaffolding (including active_record and scaffold_controller) as well as invoking my own custom generator to create an additional ThingForm that I want to be a part of my scaffolding.
I created a custom generator for form that works just fine by itself. bin/rails generate form Thing name:string is properly setting up an app/forms/thing_form.rb.
But in attempting to get this generator hooked into the default scaffolding generator, I'm running into different problems with different approaches I've tried.
Inspired by active_model_serializers, when I set up a hook_for :form as part of Rails::Generators::ResourceGenerator it ends up no longer invoking active_record. Here's the commit from my example repo: https://github.com/diachini/generator_hook_investigation/commit/8989b8f96b05ac1157d1d3a247bee9f0f2befe5d
To confirm I wasn't crazy, I have another branch that just confirmed generating a scaffold with active_model_serializers installed properly keeps the active_record invocation happening.
Taking a page out of this Gist explaining custom generators - I similarly attempted a hook_foron the ScaffoldGenerator and the ControllerGenerator
Just like my active_model_serializers attempt, this no longer invoked active_record.
It also began ignoring config for turning off assets and scaffold_stylesheet.
On a separate branch in my example repo: https://github.com/diachini/generator_hook_investigation/commit/86bc7b53ac34ea29d5e32d07486c73d962f86247
I saw a similar StackOverflow question with an update regarding copying the whole ScaffoldGenerator, but was hoping to avoid that approach if there's something simple that I'm missing with the hook_for approach that active_model_serializers took.

Does controllers get created whenever models are created?

I created model for user in ruby on rails using scaffold.
Then I got to know that when I saw controller's folder of the project I would find usercontroller.rb file created ? Does this means that whenever model is created controllers are created with it ?
It isn't the model creation that does it, it's the scaffolding.
http://guides.rubyonrails.org/v3.2.9/getting_started.html
Section 6 of that document describes what is generated during scaffolding. The scaffolding process creates a number of files, controller being one of them.
rails generate scaffold will generate a model, controller database migration, and views.
Here is a list of the generators that Rails provides:
assets
controller
generator
helper
integration_test
jbuilder
mailer
migration
model
resource
scaffold
scaffold_controller
task
The Ruby on Rails guides can provide you with additional information on the command line tools.

Ruby on rails without using scaffold and generator?

I am new user to ruby on rails.
I have some question please give the answer as early as possible
1) Is it possible to create web application without using *rails new application_name* command? means creating required folder and file manually?
2) I want to create application without using scaffold and generator, so everything is created manually...I searched but not get a link to do it...
You really should be using rails new (appname) to generate your project directory.
From there, you do not need to generate a scaffold. If you want to go slightly less abstract and create some things manually you can use rails generate resource (resource name).
If you want to go even less abstract then that, you can use rails generate model (model name) and rails generate controller (controller name) and rails generate migration (migration name). Within this level of abstraction, you can specify options such as methods you want the model to have or columns you want the migration to add.
And the least abstract(most manual) would be if you make these files yourself (like actually going in and creating new folders/files for models, controllers, etc.)
So on an order from most abstract to least:
1) generate scaffold
2) generate resource
3) generate model/controller/migration
4) creating the files/folders without rails
Most developers are usually working with the #2, #3, or #4 layers (remember it is always a tradeoff between eliminating a lot of time off by not having to manually create the same code over and over again and flexibility).
rails new app-name creates tons of files and folders which are necessary for the app to run. You would waste tons of time writing them yourself. Read as "reinventing the wheel".
You can simply create a controller and view file. Add the corresponding route. Start the server. Voila. You will have it. Most intro-articles showcase Scaffold to show the power of Rails i.e how much can be achieved with few lines of code.

How to create a simple Rails app with only a controller /view page ,avoiding all the unnecessary files in my app

I have a Rails app which contains a single controller(with a method) and a single view page .I would like to avoid all the unnecessary files in my app to keep this in a simple way. I need an app with controller,routes and views. So how should I scaffold my Rails 3 app, so that it contains only a minimal information?
If you are working on a real simple app, probably you the best fit would be sinatra
That is not rails, but implement with ruby.
HTH
You can manually create the files, especially when you only need a small portion of what the scaffold would create.
1) Add a statics_controller.rb to the app/controller directory. If you literally only need one page, you can simply use the index action (name your method index).
2) Add an index.html.erb (or .haml) to the app/views/statics/ directory.
3) In your config/routes.rb add the line get '/statics' => 'static#index'. You can append , as: 'your_preferred_url if you want to define your own url.
In all of the above, replace "static" with whatever name you think is appropriate. You would also need to add a model and migration if you plan to interact with a backend database.
I personally feel its good to not use scaffolding initially when you are new to rails so that you fully understand what they are doing. Scaffolding is not really doing anything fancy or magical. Its often just creating empty files in the correct directories (like I outlined above).
You can use the rails scaffold to be very specific in which part of MVC you create. For example,
$ rails generate controller Comments
or
$ rails generate model Comment commenter:string body:text post:references
I would highly recommend reading the entire Rails Getting Started Guide. But there is a specific section on generating a controller with scaffold.
Since Rails 6.1. running rails new --minimal gives you an application without all the bells and whistles like action mailer, action mailbox, action text, action job, active storage, action cable, ... and the accompanying configuration and stub files. It then has an ApplicationRecord, ApplicationController and the basics for an HTML view.

Why did Ruby on Rails deprecate the scaffold method

I am learning Ruby on Rails so I'm sure I'll find this out sooner or later.
Why would the scaffold method be deprecated in version 2 of Rails?
The scaffold method went against the spirit of scaffolding, which is meant to give you a starting point that you are supposed to build upon for your own needs. By generating the scaffold dynamically, there is nothing for you to edit.
The new way with the scaffold generator lets you edit the scaffolded files so you can use it to build what you actually need.
I am assuming you are referring to Dynamic Scaffolding, as the scaffold generator is still around and going strong.
David Heinemeier Hansson is on record as saying that Dynamic Scaffolding looked great in demos, but since the whole point of Dynamic Scaffolding was to teach people to use rails, abstracting it away in a single line of code was more a curse then a blessing, as no one uses Dynamic Scaffolding in production code . . . just for demos and tutorials.
If you have a copy of AWDWR handy, you can read about his whole explanation on about p81 in the latest(3rd) edition (I didn't want to copy paste).
You can still:
script/generate scaffold model_name
to generate your scaffolded model.
There is however still a Rails plugin out there that will do just what the scaffold method did before. It's called ActiveScaffold.
Because people thought it was supposed to be used for production, which would be a horrible idea. Instead you generate a scaffold which you can then easily edit and get it production ready from there.

Resources