Skip to content

Instantly share code, notes, and snippets.

@nemrosim
Last active August 31, 2019 13:54
Show Gist options
  • Save nemrosim/9240f505cb732d9ac8c58858788fd5d5 to your computer and use it in GitHub Desktop.
Save nemrosim/9240f505cb732d9ac8c58858788fd5d5 to your computer and use it in GitHub Desktop.
[Ruby on Rails CLI] commands
$ rails new api-project --api --no-sprockets -d postgresql
| | |
| v |
v NO Asset Pipeline |
only API v
use Postgresql as DB
$ rails generate model contact
$ rails g controller v1/contacts
$ rails new test-app
$ rails server
or
$ rails s
$ rake routes
$ rake routes | grep article
article.errors.any?
article.errors.full_message
// Scaffold generator command to create Article model,
// articles controller, views for articles and migration file to create articles
$ rails generate scaffold Article title:string description:text
$ rails generate migration create_article
Then
$ rails db:migrate
$ rails db:rollback !!! NOT BEST WAY
IF YOU MADE A MISTAKE - create new migration file like this
$ rails generate migration add_description_to_articles
// IN RAILS 4
$ rake db:migrate
// IN RAILS 5
$ rails db:migrate
// DATABASE
$ rails console
Article.all
Article
article = Article.new
article.title = "Some text"
article.save -> After this it will be saved to database
***
begin transaction
Article Create (0.8ms) INSERT INTO "articles" ("title", "created_at", "updated_at")
VALUES (?, ?, ?) [["title", "Some title"], ["created_at", "2019-08-30 14:46:34.678509"], ["updated_at", "2019-08-30 14:46:34.678509"]]
(7.7ms) commit transaction
***
// SAME IN TWO LINES
article = Article.new(title: "Some title")
article.save
// SAME IN ONE LINES
Article.create(title: "Some title")
$ exit
// UPDATE
$ rails console
Article.all
Article.find(1) // ID
article
article.title = "Another text"
article.save -> After this it will be saved to database
// Delete
Article.find(1) // ID
article
article.destroy
$ exit
# Example
<%= form_with do %>
Form contents
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment