[Ruby] Rails Tutorial cheat sheet

4 minute read

“I did a Rails Tutorial, but after two weeks I can’t remember what I did.”

“What can I do after all?” “I…weak…!”

It is a cheat sheet for such people.

Let’s save it for the time being, copy and paste it secretly, and add various things to make your own cheat sheet.

Functional requirements

[Rails] Search function https://qiita.com/shin1rok/items/779e581e9d12a92310c3 [Rails] Text posting function https://railstutorial.jp/chapters/user_microposts?version=5.1#cha-user_microposts [Rails] I want to post images (external library-CarrierWave) https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-basic_image_upload [Rails] The posted image gets bigger https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-image_validation [Rails] How to save images in production environment → It is better to use cloud storage. https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-image_upload_in_production [Rails] Follow and follower framework https://railstutorial.jp/chapters/following_users?version=5.1#sec-the_relationship_model [Rails] Follow, I want to make a follower list page https://railstutorial.jp/chapters/following_users?version=5.1#sec-following_and_followers_pages [Rails] I want to make a follow button (redirect and Ajax) https://railstutorial.jp/chapters/following_users?version=5.1#sec-a_working_follow_button_the_standard_way https://railstutorial.jp/chapters/following_users?version=5.1#sec-a_working_follow_button_with_ajax [Rails] Timeline function https://railstutorial.jp/chapters/following_users?version=5.1#sec-the_status_feed [Rails] Timeline, feed https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-a_proto_feed [Rails] An email address will be sent when you register new https://railstutorial.jp/chapters/account_activation?version=5.1#cha-account_activation [Rails] You can reset your password if you forget it https://railstutorial.jp/chapters/password_reset?version=5.1#cha-password_reset [Rails] Success! I failed! The one that tells you-a flash message https://railstutorial.jp/chapters/sign_up?version=5.1#sec-signup_error_messages https://railstutorial.jp/chapters/basic_login?version=5.1#sec-rendering_with_a_flash_message [Rails] Login, logout function (by yourself) https://railstutorial.jp/chapters/basic_login?version=5.1#cha-basic_login [Rails] Login, logout function (external library-devise) https://qiita.com/cigalecigales/items/f4274088f20832252374 [Rails] I want to keep logged in even after logging in and closing the browser! -Remember me https://railstutorial.jp/chapters/advanced_login?version=5.1#sec-remember_me [Rails] I want to put a thumbnail image-Gravatar https://railstutorial.jp/chapters/sign_up?version=5.1#sec-a_gravatar_image [Rails] I want to display, update or delete the edit screen https://railstutorial.jp/chapters/advanced_login?version=5.1#sec-remember_me [Rails] I want to restore the input value if it is not what I expected -Determine if it is what you expect → regular expression, validation https://railstutorial.jp/chapters/modeling_users?version=5.1#sec-user_validations -Undo → Redirect https://railstutorial.jp/chapters/sign_up?version=5.1#sec-unsuccessful_signups

Non-functional requirements

Model system

[Rails] ActiveRecord doesn’t guarantee uniqueness at the database level (even if uniqueness is guaranteed by validate) -> solve by adding index https://railstutorial.jp/chapters/modeling_users?version=5.1#sec-uniqueness_validation [Rails] I want to verify what is saved before saving it in the database-Validation https://railstutorial.jp/chapters/modeling_users?version=5.1#sec-user_validations

View system

[Rails] Pagination (Page split) https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-pagination [Rails] Editing Form https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-edit_form [Rails] I want to display a list https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-showing_all_users [Rails] I want to call a partial template → render ‘file name’ https://pikawaka.com/rails/render

Controller system

[Rails] Implementation when form input fails https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-unsuccessful_edits [Rails] Different use of path and url in redirect_to of Rails (similar thing) https://teratail.com/questions/204077 [Rails] I want to call a partial template → render ‘file name’ https://pikawaka.com/rails/render [Rails] Destroy Action and Security https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-the_destroy_action [Rails] Difference between new and build Both the new method and the build method create an instance, build can automatically set the user_id and create an instance. https://qiita.com/Kaisyou/items/8876f39e12631f4e5154 [Rails]CRUD(Create) https://railstutorial.jp/chapters/sign_up?version=5.1#sec-signup_form [Rails]CRUD(Read) https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-showing_all_users [Rails]CRUD(Destroy) https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-the_destroy_action

URL, routing system

[Rails] I want to display the top page in the state of localhost3000/ (3) → root https://railstutorial.jp/chapters/static_pages?version=5.1#sec-setting_the_root_route

[Rails] RESTful routing https://railstutorial.jp/chapters/sign_up?version=5.1#table-RESTful_users https://qiita.com/NagaokaKenichi/items/0647c30ef596cedf4bf2

I want to add a new action other than RESTful to RESTful routing using [Rails] resource. https://qiita.com/ebihara99999/items/37afb1486442e7c16a8a https://railstutorial.jp/chapters/following_users?version=5.1#sec-stats_and_a_follow_form

DB, ActiveRecord system

[Rails] I want to change the column name https://qiita.com/libertyu/items/93acd8733e34b1d0a63c

[Rails] Create seed (I want to create a lot of sample users in DB) https://qiita.com/takehanKosuke/items/79a66751fe95010ea5ee https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-sample_users[Rails] Delete migration file https://qiita.com/tanaka-t/items/cd6aa0526725e88f5024

[Rails] I want to improve the readability of ActiveRecord SQL → Scope https://qiita.com/ngron/items/14a39ce62c9d30bf3ac3

[Rails] I want to search across multiple tables https://qiita.com/leon-joel/items/f26556c9e56833983856 https://qiita.com/makitokezuka/items/f13b2e7bad77b5594911

[Rails] Index By creating an index, only the necessary data is stored separately from the table in a state optimized for searching separately from the table. In addition to sorting for search, you can search only the columns that have indexes, so you can perform high-speed searches. Disadvantage is that it takes time to add data because a search table is saved separately from the table. Because when you add the data you have to add two tables. https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-a_micropost_model https://www.dbonline.jp/sqlite/index/index1.html https://ja.wikipedia.org/wiki/%E7%B4%A2%E5%BC%95_%28%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99 %E3%83%BC%E3%82%B9%29

[Rails] I want to be able to delete children when the parent is deleted dependent: :destroy https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-dependent_destroy

[Rails] How to call data of another table at the time of relation https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-destroying_microposts

[Rails] Preventing a user from following the same user more than once-composite key index https://railstutorial.jp/chapters/following_users?version=5.1#sec-a_problem_with_the_data_model

Security system

[Rails] Strong parameters https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-revisiting_strong_parameters [Rails] Mass Assignment → Save request data as is in the database (weak security) https://thinkit.co.jp/story/2015/09/03/6389

[Rails] I want to undo my work! rails destroy controller ~ https://railstutorial.jp/chapters/static_pages?version=5.1#sec-generated_static_pages

[Rails] Activate account https://railstutorial.jp/chapters/account_activation?version=5.1#cha-account_activation

[Rails] SSL Technology that encrypts important information before it flows from the local server to the network https://railstutorial.jp/chapters/sign_up?version=5.1#sec-ssl_in_production

[Rails] CSRF measures [Controller] protect_from_forgery with: :exception https://qiita.com/tanaka7014/items/5b4e2204dc6bec83c90e

[Rails] Placeholder https://wa3.i-3-i.info/word118.html

[Rails] I want to save the password by hashing it, encrypting it https://railstutorial.jp/chapters/modeling_users?version=5.1#sec-adding_a_secure_password

Error system

[Rails] I want to display an error message on the browser screen https://railstutorial.jp/chapters/basic_login?version=5.1#sec-rendering_with_a_flash_message https://railstutorial.jp/chapters/sign_up?version=5.1#sec-signup_error_messages https://railstutorial.jp/chapters/sign_up?version=5.1#sec-the_flash

Timing system

[Rails] I want to lowercase email addresses before saving them in the database-Callback https://qiita.com/okamoto_ryo/items/458097542e826623b7ad https://railstutorial.jp/chapters/account_activation?version=5.1#sec-activation_token_callback Callback. It refers to a mechanism that adds common processing before and after an object is created (created), updated (updated), destroyed (deleted), or when validation is executed. before_save {self.email = email.downcase} Pass the block to set the user’s email address before_create :create_activation_digest The code above is called a method reference, which will cause Rails to look for a method called create_activation_digest and execute it before creating the user.

[Rails] I want to display the page only to logged in users Controller before_action :logged_in_user, only: [:edit, :update] https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-authorization

[Rails] I want to edit only myself https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-requiring_the_right_user

[Rails] A function to redirect the user to the page they were browsing immediately after logging in. →I want to access the URL of the login user’s page →Log in →Go to the login page I saw earlier Friendly forwarding request.url https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-friendly_forwarding [Rails] I want to return the previous URL (I want to redirect) request.referrer https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-destroying_microposts [Rails] Difference between authorization and authentication https://qiita.com/kaysquare1231/items/c4e4736f2a924b03777b

I want to write it in DRY (readability, maintainability)

[Rails] I want to combine similar code into one (partial) https://railstutorial.jp/chapters/filling_in_the_layout?version=5.1#sec-partials

[Rails] I want to write DRY code, but part of it is different https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-edit_form

[Rails] DRY variable element of title https://railstutorial.jp/chapters/static_pages?version=5.1#sec-layouts_and_embedded_ruby https://qiita.com/shumpeism/items/a0ad5930fa3bc0d24c70

[Rails] Private method is defined in controller, but it is so versatile that I want to use the method defined in different Controller (inheritance → application_controller.rb) https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-micropost_access_control

[Rails] Notation list https://qiita.com/gakkie/items/3afcd505c786364aa5fa https://blog.mothule.com/ruby/ruby-percent-syntax

[Ruby] Notation list https://railstutorial.jp/chapters/rails_flavored_ruby?version=5.1#cha-rails_flavored_ruby

[Rails] Difference between keyword argument and optional argument https://railstutorial.jp/chapters/updating_and_deleting_users?version=5.1#sec-users_index