質問編集履歴

4

model情報の追加

2023/03/16 10:57

投稿

sato_sato
sato_sato

スコア8

test CHANGED
File without changes
test CHANGED
@@ -51,6 +51,32 @@
51
51
  book GET /books/:id(.:format) books#show
52
52
  user GET /users/:id(.:format) users#show
53
53
  ```
54
+ ```models/user.rb
55
+ devise :database_authenticatable, :registerable,
56
+ :recoverable, :rememberable, :validatable
57
+
58
+ has_many :books, dependent: :destroy
59
+ has_one_attached :profile_image
60
+
61
+ validates :name, presence: true, uniqueness: true, length: { in: 2..20 }
62
+ validates :introduction, length: { maximum: 50 }
63
+
64
+ def get_profile_image(width, height)
65
+ unless profile_image.attached?
66
+ file_path = Rails.root.join('app/assets/images/no_image.jpg')
67
+ profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg')
68
+ end
69
+ profile_image.variant(resize_to_limit: [width, height]).processed
70
+ end
71
+ ```
72
+ ```models/book.rb
73
+ belongs_to :user
74
+ has_one_attached :image
75
+
76
+ validates :title, presence: true
77
+ validates :body, presence: true, length: { maximum: 200 }
78
+ ```
79
+
54
80
 
55
81
  ### 試したこと
56
82
  ###### ①番号リスト複数インスタンスを1つの配列に加えるように記述してみました

3

「試したこと」③の追記

2023/03/15 16:36

投稿

sato_sato
sato_sato

スコア8

test CHANGED
File without changes
test CHANGED
@@ -85,7 +85,12 @@
85
85
  @users
86
86
 
87
87
  ---
88
+ ###### ②|books|、|users| → |book|、|user|へ変更
89
+ エラーは解消されましたが、下記の画像のように2回投稿されたことになっている(1つ削除すると2段分削除されたため、1投稿としてカウントされている)
90
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-03-16/4bf9d53c-8b05-4727-8710-6f577e23114c.png)
91
+
92
+ ---
88
- ###### eachメソッド | user |を削除
93
+ ###### eachメソッド | user |を削除
89
94
  ```views/books/index.html.erb
90
95
  <% @books.each do |books| %>
91
96
  <tr>

2

「試したこと」②を追記しました。

2023/03/15 16:30

投稿

sato_sato
sato_sato

スコア8

test CHANGED
File without changes
test CHANGED
@@ -53,7 +53,7 @@
53
53
  ```
54
54
 
55
55
  ### 試したこと
56
- 複数インスタンスを1つの配列に加えるように記述してみました
56
+ ###### ①番号リスト複数インスタンスを1つの配列に加えるように記述してみました
57
57
  ```controllers/books_controller.rb
58
58
  def index
59
59
  @user = current_user
@@ -77,13 +77,32 @@
77
77
  </tr>
78
78
  <% end %>
79
79
  ```
80
-    
80
+
81
81
  エラーメッセージ
82
82
  undefined local variable or method `user' for #<ActionView::Base:  >
83
83
  Did you mean?
84
84
  @user
85
85
  @users
86
86
 
87
+ ---
88
+ ###### ②eachメソッド | user |を削除
89
+ ```views/books/index.html.erb
90
+ <% @books.each do |books| %>
91
+ <tr>
92
+ <td>
93
+ <%= link_to user_path(user.id) do %>
94
+ <%= image_tag book.user.get_profile_image(100,100) %>
95
+ <% end %>
96
+ </td>
87
- ---------------------------------------------------------------------------------------
97
+ <td><%= link_to book.title, book_path(book.id) %></td>
98
+ <td><%= book.body %></td>
99
+ </tr>
100
+ <% end %>
101
+ ```
102
+   ↓
103
+ <%= link_to user_path(user.id) do %>にName error
104
+
105
+ ---
106
+ ---
88
107
  提供コード等に不足ありましたらご指摘いただけると幸いです。
89
108
  よろしくお願いいたします。

1

「試したこと」を追記しました。

2023/03/15 15:35

投稿

sato_sato
sato_sato

スコア8

test CHANGED
File without changes
test CHANGED
@@ -51,6 +51,39 @@
51
51
  book GET /books/:id(.:format) books#show
52
52
  user GET /users/:id(.:format) users#show
53
53
  ```
54
+
55
+ ### 試したこと
56
+ 複数インスタンスを1つの配列に加えるように記述してみました
57
+ ```controllers/books_controller.rb
58
+ def index
59
+ @user = current_user
60
+ @users = User.all
61
+ @book = Book.new
62
+ @books = Book.all
63
+ # 複数モデルのインスタンスを1つの配列へ
64
+ @instances = @users | @books
65
+ end
66
+ ```
67
+ ```views/books/index.html.erb
68
+ <% @instances.each do |instance| %>
69
+ <tr>
70
+ <td>
71
+ <%= link_to user_path(user.id) do %>
72
+ <%= image_tag book.user.get_profile_image(100,100) %>
73
+ <% end %>
74
+ </td>
75
+ <td><%= link_to book.title, book_path(book.id) %></td>
76
+ <td><%= book.body %></td>
77
+ </tr>
78
+ <% end %>
79
+ ```
80
+    ↓
81
+ エラーメッセージ
82
+ undefined local variable or method `user' for #<ActionView::Base:  >
83
+ Did you mean?
84
+ @user
85
+ @users
86
+
54
87
  ---------------------------------------------------------------------------------------
55
88
  提供コード等に不足ありましたらご指摘いただけると幸いです。
56
89
  よろしくお願いいたします。