・ Rails tutorial is the 4th edition ・ This study is the 3rd lap (2nd lap after Chapter 9) ・ The author is a beginner who has done all of Progate.
・ If you read it, you will not understand it. ・ Search and summarize terms that you do not understand (at the bottom of the article, glossary). ・ Dive into what you do not understand. ・ Work on all exercises. ・ Do not copy chords as much as possible.
Authentication system development, 5th stage, finally double-digit, Chapter 10 has entered. Let's complete the REST action. Click here for today's BGM. PLASTIC GIRL IN CLOSET "TOY" Will it be an album 10 years ago ... Time goes by quickly. Sora, my twenties are over.
-If you put target = "\ _ blank" in the a tag of the link, the link will be opened in another tab. (Security measures are exercises) -Rails internally determines whether it is a new user or an existing user using the new_record method of Active Record.
<a href="http://gravatar.com/emails" target="_blank" rel="noopener">change</a>
2. Let's refactor the new.html.erb view (Listing 10.6) and the edit.html.erb view (Listing 10.7) using the partials in Listing 10.5 (let's remove the code duplication). Tip: You can use the provide method used in 3.4.3 to remove the duplicates 3. (If you have already solved the exercise in related list 7.27, you may not be able to solve this exercise well. If you are not, try to tackle this exercise while thinking about what is different in your existing code. Let's say, for example, I would use the technique of passing variables used in Listing 10.5 to pass the URLs needed in Listing 10.6 and Listing 10.7 to Listing 10.5.) → Just do it. (For some reason, I was doing the exercises in Chapter 7, but there was no difference between the views. Did I return it when I was thinking?)
assert_select "div.alert", "The form contains 4 errors."
Let's send valid information and check whether the editing actually succeeds.
If you change to an appropriate email address that is not linked to Gravatar (such as [email protected]), how will your profile image be displayed? Actually change the email address from the edit form and change it. Let's check. → Collectively, change the address of the sample user appropriately → Success, the default image of Gravatar is displayed.
Authentication: Identifies the user of the site Authorization: Manages the operations that a user can perform
before_action: In the controller, execute a specific method just before any action is executed. Apply only to a specific action by passing only: [: action] to the option.
2. Which of the above actions can be easily tested in a browser? → Edit, right? Because the HTTP request is GET. It will be displayed in the browser.
request object: Refer to Rails Guide. Besides the URL, it also contains various client-side information.
users_edit_test.rb
test "successful edit with friendly forwarding" do
get edit_user_path(@user)
assert_equal session[:forwarding_url], edit_user_url(@user)
log_in_as(@user)
assert_redirected_to edit_user_url(@user)
assert_nil session[:forwarding_url]
assert_redirected_to edit_user_url(@user)
name = "Foo Bar"
email = "[email protected]"
patch user_path(@user), params: { user: {name: name,
email: email,
password: "",
password_confirmation: "" } }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal name, @user.name
assert_equal email, @user.email
end
2. Let's put the debugger method introduced in 7.1.3 in the new action of the Sessions controller. Then log out and try accessing / users / 1 / edit (the debugger should stop prematurely). Now let's move to the console and check if the value of session [: forwarding_url] is correct. Also, check the value of request.get? When accessing the new action (when using the debugger, the terminal sometimes stops at unexpected places or behaves strangely. Skilled I feel like I've become a developer of (Column 1.1), so let's calm down and deal with it). → If you enter session [: forwarding_url] in (byebug), the stored URL (~ / users / 1 / edit) will be displayed, and if you enter request.get ?, true will be returned.
site_layout_test
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
assert_select "a[href=?]", signup_path
get contact_path
assert_select "title", full_title("Contact")
get signup_path
assert_select "title", full_title("Sign up")
log_in_as(@user)
follow_redirect! #Or get user_path(@user)
assert_select "a[href=?]", users_path
assert_select "a[href=?]", user_path(@user)
assert_select "a[href=?]", edit_user_path(@user)
assert_select "a[href=?]", logout_path
end
end
** Differences from other exercise summaries I examined ** -The logged-in user test is separated from the non-logged-in test. ⇨ It may be better to separate this considering the readability of the code. After that, is it more reliable as a test to clearly separate the operations?
-Get root_path after logging in. ⇨ Isn't this unnatural? Does the default behavior after login jump to the user page? Why bother to go home? So, since log_in_as makes a post request, I thought that I could actually go to that page with follow_redirect! And test it, or I could specify the page to be tested with get user_path (@user). If a problem appears later, we will review it.
When I put the faker gem here, red letters appeared, so bundle update. I'm getting used to this kind of thing.
>> User.paginate(page: nil)
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT ? OFFSET ? [["LIMIT", 11], ["OFFSET", 0]]
(0.2ms) SELECT COUNT(*) FROM "users"
=> #<ActiveRecord::Relation [#<User id: 1, name: "Example User", email: "[email protected]",
Below, omitted because it is long
2. What class is the pagination object acquired in the previous exercise? Also, how is it different from the User.all class? Compare it. → User :: ActiveRecord_Relation class. It's the same.
>> User.paginate(page: nil).class
=> User::ActiveRecord_Relation
>> User.paginate(page: nil).class.superclass
=> ActiveRecord::Relation
>> User.all.class
=> User::ActiveRecord_Relation
There are many other methods of pagination, such as Kaminari and Pagy. (Let's try it in the future)
2. I commented out both of them earlier, but if you comment out only one, let's make sure that the test remains green. What test should I add if I want to test that both will_paginate links exist? Tip: Refer to Table 5.2 and add a test to count the number. Let's do it. → Since it is still GREEN, count: 2 is added.
users_index_test.rb
test "index including pagination" do
log_in_as(@user)
get users_path
assert_template 'users/index'
assert_select 'div.pagination', count: 2
User.paginate(page: 1).each do |user|
assert_select 'a[href=?]', user_path(user), text: user.name
end
end
users_controller_test.rb
test "should not allow the admin attribute to be edited via the web" do
log_in_as(@other_user)
assert_not @other_user.admin?
patch user_path(@other_user), params: {
user: { password: @other_user.password,
password_confirmation: @other_user.password,
admin: true } }
assert_not @other_user.reload.admin?
end
What? But will it be GREEN? Did the people who wrote the answer really become RED? ?? When I looked it up, This article. @ other_user.password If you do it, you can't do it! So when I changed it to "password", it became RED. Then remove: admin from permit and the test is GREEN.
-Implemented edit, update, delete. -Display the user list with index. Implement pagination with gems. -Implement user management authority by assigning admin attribute. Now you can use admin? Which returns a logical value. -Execute a specific method before a specific action with befoure_action. (There is also after_action) -Friendly forwarding = Store the page request (GET only) that you wanted to go to in the session and redirect after logging in. After that, the stored session is deleted. -Create sample data (user) in db / seeds.rb.
Chapter 10 which had a certain volume is over. Now you have implemented all the basic functions. There are some situations where you have trouble with exercises, but there is nothing you can't understand if you look it up. Let's work calmly. Next next! Chapter 11! … Ah, an authentication function that uses an email address… (distant eyes). The reason for this will be explained in the following chapters.
⇨ Go to Chapter 11! ⇦ Click here for Chapter 9 Click here for premise and author status for learning
・ Forwarding To transfer something. Is friendly forwarding a "kind transfer"?
Recommended Posts