class Product < ApplicationRecord include Hashid::Rails end
routes.rb
Rails.application.routes.draw do root to: 'homes#top' devise_for :users, controllers: { confirmations: 'users/confirmations', passwords: 'users/passwords', registrations: 'users/registrations', sessions: 'users/sessions' } resources :users, only: %i(show) resources :products end
以上のようにユーザーにProductのidを知られないようにgem'hashid-rails'を使用しているのですが、Rspecのテスト時にエラーが起きてしまいます。
describe "GET #show" do context "when at login" do before do sign_in(@user) @product = FactoryBot.create(:product) end it "returns 200 response" do product_hash_id = @product.to_param get :show, params: { hashid: product_hash_id } expect(response).to have_http_status 200 end end end
上記のような記述ですが、
1) ProductsController GET #show when at login returns 200 response Failure/Error: get :show, params: { hashid: product_hash_id } ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"products", :hashid=>"pJ0uaX"} # ./spec/controllers/products_controller_spec.rb:100:in `block (4 levels) in <main>' # -e:1:in `<main>'
とエラーが出てしまいます。
id: product_hash_id と記述した場合
Failure/Error: @product = Product.find(params[:hashid]) ActiveRecord::RecordNotFound: Couldn't find Product without an ID
product: product_hash_id と記述した場合
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"products", :product=>"Ekbunp"}
この場合の記述を教えていただきたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/11 14:46
2019/06/11 15:03