Scaffolding on Rails 6.1 api based app with versioning on controller - ruby-on-rails

I am learning to build a Rails API app with the 6.1 version. I created a rails app in the following way
rails new book-gallery --api --mysql
The app created successfully. I proceeded next doing the following
rails g scaffold Author name:string country:string
This created the controller and model with the crud. But I want API to be versioned instead
Requirement:
/v1/authors
If I pass versioning on the scaffold, the model is also getting versioned which should not be
rails g scaffold v1/Author name:string country:string
The controller path is correct, but the model I got v1.rb and folder of name v1.
I don't need versioning in the model, I am trying to keep it as author.rb
Any guidance will be grateful.
Thank you

You can't achieve what you want with 1 command.
You could do a scaffold_controller and create the model without scaffolding:
rails g model Author name country
rails g scaffold_controller v1/Author
You can also skip the :string for the model attributes. Without given the datatype it will set it to string by default.

Related

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.

How to make database on ruby on rails?

I have just created the project on ruby on rails. I also generated a controller with an instruction on bash, "rails g controller home".
However, I didn't make database. How can I generate database? Please inform me how to make database.
Thank you.
You can generate a database table by typing:
rails g model myTableName
Because your starting out new i think it would be best to try something like:
rails g scaffold Post name:string content:text
This will link the controller, the model(which is the database) and the views together, giving you a better understanding on how the MVC structure works in rails.

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