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.
Related
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.
I already have the database tables and I need to create the rails script
rails generate scaffold ...
Is there a way or a tool ?
Thanks.
EDIT: usually I do
rails generate scaffold myObj1 columna:id, columnB:string
rails generate scaffold myOdb2 columna:id, columnB:string
and it create everything including dbg script.
I want to create the script from the db now. because I already have the db tables.
Just run the scaffold, and delete the migration file it creates, since you already have the table in the database.
I am new to Ruby on Rails . This might be a very foolish question.
I have created a migration using
rails generate migration Kapol name:string position:integer
rake db:migrate
Then using phpmyadmin i copied the database already present
I then opened up rails console
My question is can i use the method Kapol.find(1)??
because when i tried it using singular or plural it says
unitialized constant:Kapol
I know that there has to be a method but where to specify it?
You must generate a model in case to create a table for it, because the migration is usually used to modify existing tables.
It might be confusing that the model generator also creates migration file in your migrations folder. The only difference is that the model generator also generates initial code to create table, on the other hand, the migration generator creates only migration file without initial code.
rails generate model Kapol name:string position:integer
More information: http://guides.rubyonrails.org/getting_started.html#generating-a-model
If you're very new to Ruby on Rails, probably the best thing for you to do is create a scaffold, which gives you your migration file, your model file, your controller file, and various view files, test files, etc. etc., which all work well together. Then you can play with these and build up from there.
rails generate scaffold Kapol name:string position:integer
If you're happy with the migration that was automatically generated, then rake db:migrate and you're all set.
As Andrew says below, you can also just generate any of those files one at a time by replacing 'scaffold' with 'model', etc.
Your Kapol.find(1) is correct.
My setup: Rails 3.0.9, Ruby 1.9.2
I wish to generate a scaffold only for the create action, what's the syntax for that? I'm guessing it's something I append to
rails g scaffold Project name:string ...
I don't believe you can. I'm looking at the generator and I see nothing related to using an option to limit the template.
Generator
Template
Rails scaffolding is a very basic tool, it's not meant to be relied on, once you get a handle on the way rails works. However, you can use Ryan Bates' nifty-generator gem to generate scaffolds with greater control. Example:
rails g nifty:scaffold post name:string index new edit
Learn more here. https://github.com/ryanb/nifty-generators
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.