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.
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
Use <% if user_signed_in?%> ← user_signed_in? is a method provided by devise, so you don't have to define it.
Accept registration only for the person's email address
-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
routes.rb
if Rails.env.development?
mount LetterOpenerWeb::Engine, at: "/letter_opener"
end
Call the view folder prepared by devise with rails g devise: views
● 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 Add
controllers: {registrations:'users / registrations'}after devise_for: users ● Creating a controller to display the user details page
rails g controller users`
● Show action definition, routing, view file creation
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.
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.
● 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
● 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