CRUD Operations using Scaffold
Hello, in this article I would share about CRUD operations using scaffold generator.
So, let’s create our first application. You can crate a new rails application by typing:
rails new appname
Okay, what where going to do now is use some rails provided “Magic.” So, let’s add all four of CRUD actions including article table and then article model to communicate with the table by typing in just one line to our application. Yes, we are using what’s called scaffold generator:
rails generate scaffold Article title:string description:text
And now we have to run our migration file to create that actual table:
rake db:migrate
If we look at the app directory under db > schema.rb there is the articles table:
create_table “articles”, force: :cascade do |t|
t.string “title”
t.text “description”
t.datetime “created_at”, null: false
t.datetime “updated_at”, null: false
And now look at config > routes.rb you can see resources :articles line, it give a lot of routes to our application by typing:
rake routes
Voila!
Let’s start our rails server and we take a look
rails s
Open your browser and access http://localhost:3000/articles
That’s rails gave you with just one line code, you’ve got all CRUD actions using the scaffold generator. But for that, instead of having code like this, where you have a lot of “clutter” that just got generated..and to be honest it’s a little dificult to understand it first, but if you build it from scratch, you’ll know where and what to changes to bend the code the way that you want.
Thank you.