###前提・実現したいこと
new_product_pathからedit_product_pathへredirect_toさせる際にエラーが起こってしまった。
実現させたいこと
app/products/new から app/products/:id/edit へと遷移させたい。 :idはproduct.id
###発生している問題・エラーメッセージ
ActionController::UrlGenerationError in ProductsController#new No route matches {:action=>"edit", :controller=>"products", :id=>nil} missing required keys: [:id]
###該当のソースコード
products_controller.rb
ruby
1class ProductsController < ApplicationController 2before_action :set_product, only: [:edit, :update, :destroy, :show, :preview] 3 4#GET /products/new 5 def new 6 product = current_user.products.create 7 8 redirect_to edit_product_path(product) 9 end 10 11 #GET /products/:id/edit 12 def edit 13 end 14 15private 16 17 def set_product 18 @product = Product.find(params[:id]) 19 end
route.rb
ruby
1resources :products, only: [:index, :new, :edit, :update, :show, :destroy] do 2 get 'latest', on: :collection 3 4 member do 5 get 'preview' 6 patch 'likes' 7 delete 'likes' 8 end 9 end
index.html.slim
= link_to image_tag('test_2'), root_path - if user_signed_in? .header-notification = react_component('NotificationBox') = link_to "投稿する", new_product_path = link_to fa_icon('search'), '#search', data: { toggle: 'collapse' }, aria: { expanded: 'false', controls: 'search-collapse' }
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
ruby "2.3.1" rails "5.0.1"
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/28 08:56