Last active
August 31, 2019 13:54
-
-
Save nemrosim/9240f505cb732d9ac8c58858788fd5d5 to your computer and use it in GitHub Desktop.
[Ruby on Rails CLI] commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails new test-app | |
$ rails server | |
or | |
$ rails s | |
$ rake routes | |
$ rake routes | grep article |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
article.errors.any? | |
article.errors.full_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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