質問編集履歴

4

件名を修正

2022/05/18 06:04

投稿

chichichi
chichichi

スコア10

test CHANGED
@@ -1 +1 @@
1
- ArgumentError in Prototypes#indexが解消できない
1
+ ArgumentError in Samples#indexが解消できない
test CHANGED
File without changes

3

コードを一部修正しました

2022/05/18 06:02

投稿

chichichi
chichichi

スコア10

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,13 @@
2
2
  アプリケーションがほぼ完成し、動作の見直しをしていた際、それまで正常に動いていた投稿内容の「編集」ボタンを押した際にエラーが発生し、エラー解消railのためにいろいろいじっていたところ、今度はindexを表示できなくなってしまった。
3
3
 
4
4
  # エラー内容
5
- #### ArgumentError in Prototypes#index
5
+ #### ArgumentError in Samples#index
6
6
  Can't resolve image into URL: to_model delegated to attachment, but attachment is nil
7
7
  原因は部分テンプレートの以下の部分で発生しているらしい。
8
- <%= link_to image_tag(prototype.image, class: :card__img), prototype_path(prototype.id) %>
8
+ <%= link_to image_tag(sample.image, class: :card__img), sample_path(sample.id) %>
9
- ```views/prototypes/_prototype.html.erb
9
+ ```views/samples/_sample.html.erb
10
10
  <div class="card">
11
- <%= link_to image_tag(prototype.image, class: :card__img), prototype_path(prototype.id) %>
11
+ <%= link_to image_tag(sample.image, class: :card__img), sample_path(sample.id) %>
12
12
  省略
13
13
  ```
14
14
 

2

解決に不要な部分を削除しました

2022/05/16 11:17

投稿

chichichi
chichichi

スコア10

test CHANGED
File without changes
test CHANGED
@@ -9,116 +9,9 @@
9
9
  ```views/prototypes/_prototype.html.erb
10
10
  <div class="card">
11
11
  <%= link_to image_tag(prototype.image, class: :card__img), prototype_path(prototype.id) %>
12
- <div class="card__body">
13
- <%= link_to prototype.title, prototype_path(prototype.id), class: :card__title%>
14
- <p class="card__summary">
15
- <%= prototype.catch_copy %>
12
+ 省略
16
13
  ```
17
14
 
18
-
19
-
20
- ```routes.rb
21
- Rails.application.routes.draw do
22
- devise_for :users
23
- root to: "prototypes#index"
24
- resources :prototypes do
25
- resources :comments, only: :create do
26
- end
27
- end
28
- resources :users, only: :show
29
- end
30
- ```
31
- ```models/prototype.rb
32
- class Prototype < ApplicationRecord
33
- belongs_to :user
34
- has_one_attached :image
35
- has_many :comments, dependent: :destroy
36
-
37
- validates :title, presence: true
38
- validates :catch_copy, presence: true
39
- validates :concept, presence: true
40
- validates :image, presence: true
41
- end
42
- ```
43
- ```prototypes.controller.rb
44
- class PrototypesController < ApplicationController
45
- before_action :authenticate_user!, except: [:index, :show]
46
- before_action :move_to_index, except: [:index, :new, :create, :show, :destroy]
47
-
48
- def index
49
- @prototypes = Prototype.all.includes(:user)
50
- end
51
-
52
- def new
53
- @prototype = Prototype.new
54
- end
55
-
56
- def create
57
- @prototype = Prototype.new(prototype_params)
58
- if @prototype.save
59
- redirect_to root_path(@prototype)
60
- else
61
- render :new
62
- end
63
- end
64
-
65
- def show
66
- @prototype = Prototype.find(params[:id])
67
- @comment = Comment.new
68
- @comments = @prototype.comments.includes(:user)
69
- end
70
-
71
- def edit
72
- @prototype = Prototype.find(params[:id])
73
- end
74
-
75
- def update
76
- @prototype = Prototype.find(params[:id])
77
- if @prototype.update(prototype_params)
78
- redirect_to prototype_path(@prototype.id)
79
- else
80
- render :edit
81
- end
82
- end
83
-
84
- def destroy
85
- @prototype = Prototype.find(params[:id])
86
- if @prototype.destroy
87
- redirect_to root_path
88
- end
89
- end
90
-
91
- private
92
- def prototype_params
93
- params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id)
94
- end
95
-
96
- def move_to_index
97
- @prototype = Prototype.find(params[:id])
98
- unless @prototype.user_id == current_user.id
99
- redirect_to root_path
100
- end
101
- end
102
- end
103
- ```
104
- ```views/prototypes/index.html.erb
105
- <main class="main">
106
- <div class="inner">
107
- <% if user_signed_in? %>
108
- <%# ログインしているときは以下を表示する %>
109
- <div class="greeting">
110
- こんにちは、
111
- <%= link_to "#{current_user.name}さん", user_path(id: current_user.id), class: :greeting__link %>
112
- </div>
113
- <% end %>
114
- <%# // ログインしているときは上記を表示する %>
115
- <div class="card__wrapper">
116
- <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %>
117
- <%= render @prototypes, collection: @prototypes %>
118
- </div>
119
- </div>
120
- </main>
121
- ```
122
15
 
123
16
  # 自分で調べたことや試したこと
124
17
  「to_model は添付ファイルに委任されています」とは、アソシエーションのhas_one_attachedが効いてるというメッセージ。にも関わらず、添付ファイルはnilですと返ってくる。

1

コメントありがとうござあいます。回答させていただきました。

2022/05/16 10:44

投稿

chichichi
chichichi

スコア10

test CHANGED
File without changes
test CHANGED
@@ -130,3 +130,4 @@
130
130
 
131
131
  丸一日見直していましたが原因を見つけられず。
132
132
  どなたかコードを見ていただけると幸いです。
133
+