How do I define a scaffold template? - ruby-on-rails

Each time I generate a scaffold I need to customize the stylesheet. So it takes much more time to update style for index, form and show page. If I paste the predefined template code then I will have to change rails code instance variables and path.
Is there any way to define a specific style for each and every scaffold so that I used this to save time and overhead.
In short, I want to layout my page generate from scaffold like my form, index and show page with a predefined style.
I want to use custom mark-up and stylesheet in the generated scaffold. I want to do a one time definition of this scaffold template and use it every time I generate a new scaffold.

I partially solved my problem though .
rails g bootstrap:layout [LAYOUT_NAME]
and
rails g bootstrap:themed [RESOURCE_NAME]
generate a scaffold by this command
rails g scaffold product name:string description:text
then implement bootstrap theme on your scaffold through this command
rails g bootstrap:themed products -f
pass the -f option to force it to overwrite the generated view files
you can generate a layout through command.
rails g bootstrap:layout appstructure
this file will be save in view/layout and reflect on each and every page.

Related

Ruby on Rails - How to get the value of the table column comment property in the scaffolding template?

I'm customizing the _form.html.erb.tt template that scaffoding uses to generate the views. But I need to get the value of the comment property of the table column to better customize each html tag of the table columns. I do this customization by running rails g scaffold the first time, then I remove the views and add column comments to the migrate, then I run rails db:migrate, then I run rails g scaffold with the --skip --skip-collision-check options for generate only the views.
In pry I can see the value of the column comment by the model.
Person.column_for_attribute('name').comment.
But I am not able to do this in the template.

Rails: How can I regenerate all the views created by scaffold after I make changes to the underlying table?

New to Rails, so please feel free to edit my question should I get the terminology wrong!
I've created my MVC via scaffold, but have since made changes to the underlying table (renamed some columns, added some columns). Is there a way to regenerate the views that were created with the original scaffold such that they reflect the changes I've made?
If I run rails g scaffold Foo --migration=false --skip I get an error that my model name is already used in my application.
If delete the entire corresponding views folder (the one created by the original scaffold), and I run rails g scaffold Foo --migration=false --force the views generated still don't pick up all the fields I have in my model.
You could reverse the scaffold generation and regenerate a new scaffold with renamed and new columns.
If you already migrated the database you will need to roll it back.
rake db:rollback
Then you can reverse the scaffold generation.
rails destroy scaffold MyFoo
Then generate new scaffold with updated columns
rails generate scaffold MyFoo column_name_1:data_type column_name_2:data_type column_name_3:data_type
Otherwise you can manually rewrite your views and controllers to include the updated and new columns.

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?

rails generate scaffold with --migration=false

I have a very large model (say 200 fields), so it isn't very convenient to write them down into the command line.
So I first generate the migration then i did scaffold with:
rails generate scaffold myModel --migrate=false
It generated the controller with its actions, it updated the routes.rb and created views/myModel.
But it didn't add the fields to the views. Isn't scaffold supposed to provide a basic field presentation? If not, is there a way to provide it?
Scaffold uses the command line arguments you specify to create the relevant files. If you don't specify any fields at the command line then it can't add them to the views (because it doesn't know them). The Scaffold command is a one time thing. You might consider typing out and/or programmatically creating the relevant scaffold command in a text editor and pasting it into the console.

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

Resources