質問編集履歴

2

文法の修正

2019/04/25 22:20

投稿

gaijin
gaijin

スコア30

test CHANGED
File without changes
test CHANGED
@@ -111,3 +111,87 @@
111
111
  undefined local variable or method `artist_id' for #<TrucksController:0x00000001cf1cb8> Did you mean? artist_index_url artist_index_path artist_url
112
112
 
113
113
  ```
114
+
115
+
116
+
117
+ 再追記
118
+
119
+ new.html.erb
120
+
121
+ ```
122
+
123
+ <h1>Add Truck</h1>
124
+
125
+
126
+
127
+ <%= form_for (@artist, @truck]) do |f| %>
128
+
129
+ <%= hidden_field_tag "artist_id", @truck.truck_id %>
130
+
131
+ <%= f.text_field :title %><br>
132
+
133
+ <%= f.submit %>
134
+
135
+ <% end %>
136
+
137
+ ```
138
+
139
+ trucks_controller.rb
140
+
141
+ ```ここに言語を入力
142
+
143
+ class TrucksController < ApplicationController
144
+
145
+ def show
146
+
147
+ @truck = Truck.find params[:id]
148
+
149
+ end
150
+
151
+ def new
152
+
153
+ end
154
+
155
+ def create
156
+
157
+ @truck = Truck.new(truck_params)
158
+
159
+ @truck.save
160
+
161
+
162
+
163
+ redirect_to artist_truck_path(artist_id, @truck)
164
+
165
+ end
166
+
167
+ def truck_params
168
+
169
+ params.require(:truck).permit(:title)
170
+
171
+ end
172
+
173
+ end
174
+
175
+ ```
176
+
177
+ routes.rb
178
+
179
+ ```ここに言語を入力
180
+
181
+ get 'artist/index'
182
+
183
+
184
+
185
+ resources :artists do
186
+
187
+ resources :comments
188
+
189
+ resources :trucks
190
+
191
+ end
192
+
193
+
194
+
195
+ root 'artist#index'
196
+
197
+ ```

1

文法の修正

2019/04/25 22:20

投稿

gaijin
gaijin

スコア30

test CHANGED
File without changes
test CHANGED
@@ -85,3 +85,29 @@
85
85
  DELETE /artists/:artist_id/trucks/:id(.:format) trucks#destroy
86
86
 
87
87
  ```
88
+
89
+
90
+
91
+ 追記
92
+
93
+
94
+
95
+ redirect_to artist_truck_path(@artist, @truck)
96
+
97
+ ```ここに言語を入力
98
+
99
+ ActionController::UrlGenerationError in TrucksController#create
100
+
101
+ No route matches {:action=>"show", :artist_id=>nil, :controller=>"trucks", :id=>"24"} missing required keys: [:artist_id]
102
+
103
+ ```
104
+
105
+ redirect_to artist_truck_path(artist_id, @truck)
106
+
107
+ ```ここに言語を入力
108
+
109
+ NameError in TrucksController#create
110
+
111
+ undefined local variable or method `artist_id' for #<TrucksController:0x00000001cf1cb8> Did you mean? artist_index_url artist_index_path artist_url
112
+
113
+ ```