[RUBY] I tried Rails beginner [Chapter 2]

Chapter 2 User registration / login function creation

Preparing to use devise (gem)

Add gem'devise' to your Gemfile and run bundle install in your terminal After running rails g devise: install, create a user model with rails g devise user This will also automatically create the routing.

Installation of logout link

Show partial template: Add <% = render'layouts / header'%> inside the body of the app view Create partial template: Create a _header.html.erb file in the view's layouts folder

Change the link displayed when logging in and out

Use <% if user_signed_in?%> ← user_signed_in? is a method provided by devise, so you don't have to define it.

Add email authentication function

Accept registration only for the person's email address

Authentication function using devise

-Added : confirmable to the user model file -Added config.action_mailer.default_url_options = {host:" localhost ", port: 3000} to config / environments / development.rb (= settings / environment / development files)

● Check the email sent in the development environment -Add gem'letter_opener_web in group: development do of Gemfile and execute bundle install -Add config.action_mailer.delivery_method =: letter_opener_web near the bottom of the development file that you added earlier. -Add the following code to the root file


if Rails.env.development?
 mount LetterOpenerWeb::Engine, at: "/letter_opener"
⚠️ New registration is no longer possible after implementing the email authentication function
スクリーンショット 2020-06-13 18.43.33.png I was told that confirmed_at could not be found Maybe it's because rails db: migrate was done without uncommenting the migration file when creating the user model? I was able to resolve the error by referring to [here](https://teratail.com/questions/140788). Drop (delete) the database with `bin / rails db: drop`, configure and re-migrate again
Edit the text content of the email and the text of the new registration / login screen

Call the view folder prepared by devise with rails g devise: views

Create user detail page

Add name column to users table

● Execute rails g migration AddNameToUsers and add ʻadd_column: users,: name,: stringin the def change of the created migration file ➡️ migrate ● Rewriting the new registration form ● Set strong parameters -Create a controller file for the new user registration function:rails g devise: controllers users app / controllers / users / registrations_controller.rb becomes the controller for new registration. Uncomment the part about configure_sign_up_params · Rewrite the routing file to apply the customized controller file Addcontrollers: {registrations:'users / registrations'}after devise_for: users ● Creating a controller to display the user details pagerails g controller users` ● Show action definition, routing, view file creation

⚠️ Is the name column empty even after new registration?

The key of the uncommented part of the controller for new registration was not used as the name.-The cause was a spelling mistake in the root file.

Link users with questions

Have user_id and column in question table ● Create a migration file rails g migration AddUserIDToQuestions -Execute ʻadd_reference: questions,: user, foreign_key: true` ➡️ migrate in def change

● Processing to enter a value in user_id -Rewrite the create action of the question controller current_user is provided by devise

● Model association (association) -Has_many: questions in the user model file By adding belongs_to: user to the question model file, you can associate multiple questions for one user and one user for one question. ・ If you add depandent: destroy to the continuation of has_many ~ in ↑, when you delete a user, the created question will also be deleted.

● Display the title list of the created question on the user details page ・ Because I am using an association, I can get the question of that user at @ user.questions.

Create a question edit / delete function

● Routing ・ `Get" / questions /: id / edit ", to:" questions # edit " ・ Patch "/ questions /: id", to: "questions # update" ⭐️ ・ Delete "/ questions /: id", to: "questions # destroy"

● Action definition and view creation -Since it is assigned to @question in the edit action, the edit form can be created with model: @question specified in the form_with tag.

● Put a link to the edit / delete page on the question details page -Specify method :: HTTP request method, data: {confirm:" message "} in the link tag of the deleted page.

● Allow only logged-in users to edit / delete / question Use devise methods (ʻauthenticate_user! ) · Questions controller before_action: authenticate_user !, only: [: new,: create, edit, update,: destroy]`

● Only the person who created the question can edit / delete it. ・ Under ↑ before_action: ensure_correct_user, only: [: edit,: update,: destroy] -Define ensure_correct_user under private ◎ Difference between find (params [: id]) and find_by (id: params [: id]) The processing when the value is not found is different [Reference](https://doruby.jp/users/tn_on_rails/entries/-Rails-find(%3Cid%3E)%E3%81%A8find_by (id--%3Cid) % 3E)% E3% 81% AE% E9% 81% 95% E3% 81% 84) find: get an error find_by: returns nil

● Display edit / delete links only to the person who created the question

What i learned

● render in view shows partial template ● The link_to method method: specifies the type of HTTP request method. If not specified, it will be get Display an alert when a link is clicked with data: {confirm:" text "} ● ul tag = unordered list tag. Used for things that are relevant and regular (navigation menus, slide shows, etc.) ● Ruby embed code: <% =%> if you want to display it, but if you don't need to display it, you don't need equal ● If you want to modify after executing migration once, you need to roll back and then modify and execute bin / rails db: migrate. ● add_reference creates the columns needed to associate the table. _Id is automatically added to the column name Foreign key constraint (foreign_key: true): Something that is more secure when creating a foreign key ● Association is convenient ● Routing methods: patch (partial update of resources) and delete ● Bulk automatic routing resources: controller name Execute and confirm rails routes

Recommended Posts

I tried Rails beginner [Chapter 2]
[Rails] I tried deleting the application
I tried to introduce CircleCI 2.0 to Rails app
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
I tried Spring.
rails tutorial Chapter 10
I tried tomcat
rails tutorial Chapter 9
I tried youtubeDataApi.
I tried refactoring ①
rails tutorial Chapter 8
I tried FizzBuzz.
[Beginner] Rails Tutorial
I tried JHipster 5.1
I tried to organize the session in Rails
I tried writing CRUD with Rails + Vue + devise_token_auth
[Ruby basics] I tried to learn modules (Chapter 1)
[I tried] Spring tutorial
Chapter 4 Rails Flavored Ruby
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
I tried running Autoware
I tried using Gson
Rails Tutorial Chapter 3 Notes
I tried QUARKUS immediately
Rails Tutorial Chapter 3 Learning
I tried using TestNG
I tried Spring Batch
I tried using Galasa
I tried node-jt400 (Programs)
Rails Tutorial Chapter 4 Notes
I tried node-jt400 (execute)
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
[Beginner] About Rails Session
I tried node-jt400 (Transactions)
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
[Rails] I tried playing with the comment send button
[Beginner] I stumbled upon launching a project with Rails6
I introduced Docker to Rails 6, so I summarized it (beginner)
Rails6 I tried to introduce Docker to an existing application
I tried to build an environment using Docker (beginner)
I tried unit testing Rails app using RSpec and FactoryBot
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I started Java Gold (Chapter 1-1)
I tried DI with Ruby
I tried node-jt400 (IFS write)
[Rails] I tried using the button_to method for the first time
[Rails] I tried to create a mini app with FullCalendar
I tried node-jt400 (SQL Update)
I tried using azure cloud-init
I tried Spring State machine
I tried Drools (Java, InputStream)
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried the Docker tutorial!
I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails
[Rails] I tried to implement batch processing with Rake task