Ruby on Rails初心者です。
独学で学んでおります。
色々と試しているのですが、どこが問題なのかわからず苦戦しています。
ネストしていますがpathはあっていると思います。
どうか詳しい方ご教授いただけると助かります。
error
1ActionController::UrlGenerationError in TrucksController#create 2 3No route matches {:action=>"show", :artist_id=>#<Truck id: 6, title: "foge", created_at: "2019-04-24 23:09:12", updated_at: "2019-04-24 23:09:12">, :controller=>"trucks", :id=>nil} missing required keys: [:id]
rails
1class TrucksController < ApplicationController 2 def show 3 4 end 5 def new 6 end 7 8 def create 9 @truck = Truck.new(truck_params) 10 @truck.save 11 12 **redirect_to artist_truck_path(@truck) 13 end** 14 15 def truck_params 16 params.require(:truck).permit(:title) 17 end 18end
terminal
1artist_trucks GET /artists/:artist_id/trucks(.:format) trucks#index 2 POST /artists/:artist_id/trucks(.:format) trucks#create 3 new_artist_truck GET /artists/:artist_id/trucks/new(.:format) trucks#new 4 edit_artist_truck GET /artists/:artist_id/trucks/:id/edit(.:format) trucks#edit 5 artist_truck GET /artists/:artist_id/trucks/:id(.:format) trucks#show 6 PATCH /artists/:artist_id/trucks/:id(.:format) trucks#update 7 PUT /artists/:artist_id/trucks/:id(.:format) trucks#update 8 DELETE /artists/:artist_id/trucks/:id(.:format) trucks#destroy
追記
redirect_to artist_truck_path(@artist, @truck)
ActionController::UrlGenerationError in TrucksController#create No route matches {:action=>"show", :artist_id=>nil, :controller=>"trucks", :id=>"24"} missing required keys: [:artist_id]
redirect_to artist_truck_path(artist_id, @truck)
NameError in TrucksController#create undefined local variable or method `artist_id' for #<TrucksController:0x00000001cf1cb8> Did you mean? artist_index_url artist_index_path artist_url
再追記
new.html.erb
<h1>Add Truck</h1> <%= form_for (@artist, @truck]) do |f| %> <%= hidden_field_tag "artist_id", @truck.truck_id %> <%= f.text_field :title %><br> <%= f.submit %> <% end %>
trucks_controller.rb
class TrucksController < ApplicationController def show @truck = Truck.find params[:id] end def new end def create @truck = Truck.new(truck_params) @truck.save redirect_to artist_truck_path(artist_id, @truck) end def truck_params params.require(:truck).permit(:title) end end
routes.rb
get 'artist/index' resources :artists do resources :comments resources :trucks end root 'artist#index'
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/25 13:50
2019/04/25 13:53
2019/04/25 14:21 編集
2019/04/25 14:38
2019/04/25 15:54
2019/04/25 16:19
2019/04/25 22:13
2019/04/25 23:40 編集
2019/04/26 01:26
2019/04/26 03:26
2019/04/27 06:26