[RUBY] rails tutorial Chapter 10

Introduction

I will post the process of advancing the rails tutorial on my own.

It touches on words that I didn't understand in the process, jammed errors, and so on.

Please point out any mistakes as it is an output of personal learning.

Since this is my first post, I think there are many places that are difficult to read, but please forgive me.

Chapter 10 User Update / Display / Delete

10.1.1 Edit form

Exercise 2

ruby:app/views/users/_form.html.erb


#Listing 10.5:Partial for new and edit forms
<%= form_with(model: @user, local: true) do |f| %>
  <%= render 'shared/error_messages', object: @user %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>

  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation, class: 'form-control' %>

  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

Maybe

object: @user

Has been added ... I think you're probably specifying an object to pass to shared / error_messages shared / error_messages is as below

ruby:app/views/shared/_error_messages.html.erb


<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

I don't think it's necessary because I specified the @user object, but ...

reference https://teratail.com/questions/112178

Also,

<%= f.label :password_confirmation, "Confirmation" %>

The second argument of is gone ...

I could have expected the second argument to be the text of the label, but I looked it up just in case.

reference https://teratail.com/questions/112172

The text of the label is changed because the second argument is no longer specified. Confirmation says Password confirmation.

I understand both meanings, but why was it fixed silently?

10.2.3 Friendly forwarding

Q. What is a request object? A. request.original_url Returns the current request URL request.get? Returns true when HTTP method is GET

reference https://railsguides.jp/action_controller_overview.html#request%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88

10.3.2 Sample user

a problem occured!! I get an error when I run rails db: migrate: reset

Permission denied @ apply2files - C:/environment/sample_app/db/development.sqlite3
Couldn't drop database 'db/development.sqlite3'
rails aborted!
Errno::EACCES: Permission denied @ apply2files - C:/environment/sample_app/db/development.sqlite3
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:drop:_unsafe
(See full trace by running task with --trace)

I solved it by referring to this article. https://qiita.com/Toshiki23/items/f366504844fd22ad87d9

I used it as a reference before. It seems that windows cannot delete the files that it is accessing.

10.3.3 Pagination

User.paginate(page: 1)

paginate takes value page number with key: page User.paginate retrieves a chunk of data (30 by default) from the database based on the: page parameter Executing the above command on the console will output the object with page number 1 (the output will be 11, but it is due to Active Record's own console limit. You can work around this limitation by calling the length method).

@users = User.paginate(page: params[:page])

If params [: page]) is 1, 1 to 30 users will be fetched, and if params [: page]) is 2, 31 to 60 users will be fetched and assigned to @users. By the way, if the page is nil, paginate simply returns the first page.

10.3.5 Partial refactoring

ruby:app/views/users/index.html.erb


<% provide(:title, 'All users') %>
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <% @users.each do |user| %>
    <%= render user %>
  <% end %>
</ul>

<%= will_paginate %>

ruby:app/views/users/_user.html.erb


<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.name, user %>
</li>

Further refactoring.

ruby:app/views/users/index.html.erb


<% provide(:title, 'All users') %>
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <%= render @users %>
</ul>

<%= will_paginate %>

Rails guesses @users as a list of User objects. In addition, if you give a collection of users and call it, Rails will automatically enumerate the collections of users and output each user as a _user.html.erb partial. (rails tutorial Quoted from Chapter 10)

Again, a difficult omission for beginners ... Do you mean passing a collection, that is, something like multiple pieces of data?

I understand that it can be omitted, but then in _user.html.erb

ruby:app/views/users/_user.html.erb


<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.name, user %>
</li>

Is it okay for the variable user created by each method to remain as it is, and if so, what is the name of the variable user?

Can the variable name be anything? I changed the name and tested it.

ruby:app/views/users/_user.html.erb


<li>
  <%= gravatar_for aaa, size: 50 %>
  <%= link_to aaa.name, user %>
</li>

result error.

undefined local variable or method `aaa' 

The variable aaa is not defined.

What can be guessed so far (1) Is the singular version of the object passed by render automatically set? (2) Since it is an object of User class, is it automatically set with the same name?

As a test

app/controllers/users_controller.rb


def index
  @aaas = User.paginate(page: params[:page])
end

ruby:app/views/users/index.html.erb


<% provide(:title, 'All users') %>
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <%= render @aaas %>
</ul>

<%= will_paginate %>

result

ActionView::Template::Error: The @users variable appears to be empty. Did you forget to pass the collection object for will_paginate?

The @users variable is empty. Did you forget to pass the collection object to will_paginate? Was also seen.

So should the variable name of the collection object be @users?

And you would have to pass it to will_paginete (a collection object named @users) ... maybe ...

Does rails have a setting that makes various good interpretations by making the collection object of the User object the variable name users (the collection object is a plural form of the class object)?

By doing so, if the variable name is user, it will have a convenient operation such as automatically enumerating the collection.

ruby:app/views/users/_user.html.erb


<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.name, user %>
</li>

Do I have to do this? I'm guessing, but ...

Of course, it is only when you write according to the rules for that, that it will be interpreted in various ways.

At the end

This time it was a chapter that suffered from refactoring. I have left some ambiguity, so I would like to look back on it several times and proceed with learning so that I can understand it clearly.

Recommended Posts

rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
[Rails Tutorial Chapter 4] Rails-flavored Ruby
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Rails tutorial test
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Rails tutorial memorandum 2
Chewing Rails Tutorial [Chapter 2 Toy Application]
Start Rails Tutorial
[Beginner] Rails Tutorial
Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Chapter 4 Rails Flavored Ruby
Rails Tutorial cheat sheet
rails tutorial fighting notes Ⅲ
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
Rails Tutorial Records and Memorandum # 0
I tried Rails beginner [Chapter 1]
Rails Tutorial (4th Edition) Summary
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
I tried Rails beginner [Chapter 2]