Rails hooking a custom generator into scaffold generator - ruby-on-rails

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.

Related

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.

Rails generate scaffold creates blank controller

The last couple of times that I've used 'rails generate scaffold [ModelName]' everything has been generated except that the controller is blank. It contains no methods at all. It's easy enough to copy that in from other sources, but I'm wondering what is going on.
The only unique thing about this application for me is that it's using the ActiveAdmin gem.
Any suggestions for how I could get this working as expected again?
+1 to hajpoj, but there are a couple additional steps you could use to troubleshoot.
What does rails generate scaffold_controller give you? My first suggestion to be to isolate the controller generator and go from there.
Following that, I would actually look in the Rails generator code at the point of controller generation and work backwards from there. Here is (I believe) the entry point, from there, you can follow the code to where things are failing. This is, obviously, not the easiest path, but would probably teach you a lot about the rails internals.

Error regarding ActiveAdmin in Ruby on Rails when customizing the menu

Mac OS X 10.7.3 Lion,
Ruby 1.9.2,
Rails 3.2.2,
Sass 3.2.3
Following this tutorial:
http://activeadmin.info/documentation.html
Following this video tutorial
http://www.youtube.com/watch?v=tAxlrHcEg9U
I add the activeadmin gem, run bundle install, then run
rails generate active_admin:install
rails generate active_admin:resource POST
Only after creating the app/admin/posts.rb and trying to run either
db migrate
rails server
fails with the error
uninitialized constant Post NameError
with out that posts.rb file i am able to run the admin interface error free.
I tried moving the sass-rails gem out side of the :assets in my gem file and re-running bundle install as suggested in another question, but to no avail I still have the error
according to the getting started active admin tutorial "Post" is suppose to be a module name so i assume the code above is calling a class method (ActiveAdmin as the class, register as the method) and sending the module as a parameter and the block do end
Regardless the error is implying that RoR doesn't know what Post is. As if it does not exist. Being new to rails i do not know how to navigate well, meaning i do not even know where this ActiveAdmin source file is in order to dig through it for a method Post
Thank you for the consideration and your time, I appreciate it.
The linked tutorial assumes that you have already created a model named Post (and have run rake db:migrate to link it to the database). The purpose of the rails generate active_admin:resource Post command is to tell ActiveAdmin that you want it to consider the Post model in part of what it does.
Historically, you'll see models like Post and User in Rails a lot -- these are the commonly used examples of creating a blogging application (a user can create blog posts).
So, whatever models you have in your application can be registered with ActiveAdmin by replacing Post with the name of your model.
Another note: while generators like this tend to be forgiving, a Post is a model that is defined in post.rb and is linked to a SQL table called posts. Be careful with things like upper- and lower-case, and singular and plurals. In Rails they all fit together in a special way.

What is the difference between default scaffold and nifty:scaffold?

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.

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