質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

481閲覧

railsのrspecでのエラーを解決したい

shinn_sora

総合スコア13

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/03/19 08:19

やりたいこと
・rspecででいる下記のエラーを解決したい

1) ProductsController POST #create saves the new product in the database Failure/Error: params.require(:product).permit(:name, :description, :first_category_id, :second_category_id, :third_category_id, :size, :product_status, :delivery_fee, :prefecture_id, :lead_time, :price, :transaction_status, product_images_attributes: [:image]).merge(user_id: current_user.id) ActionController::ParameterMissing: param is missing or the value is empty: product # ./app/controllers/products_controller.rb:49:in `product_parameter' # ./app/controllers/products_controller.rb:28:in `create' # ./spec/controllers/products_controller_spec.rb:47:in `block (3 levels) in <top (required)>' 2) ProductsController POST #create redirects to products#index Failure/Error: params.require(:product).permit(:name, :description, :first_category_id, :second_category_id, :third_category_id, :size, :product_status, :delivery_fee, :prefecture_id, :lead_time, :price, :transaction_status, product_images_attributes: [:image]).merge(user_id: current_user.id) ActionController::ParameterMissing: param is missing or the value is empty: product # ./app/controllers/products_controller.rb:49:in `product_parameter' # ./app/controllers/products_controller.rb:28:in `create' # ./spec/controllers/products_controller_spec.rb:47:in `block (3 levels) in <top (required)>'

見た感じパラメーターに関するエラーのようなのですがどのように直せばいいか見当がつきません、、、、、、、

テストのコードは以下の通りです。

require 'rails_helper' describe ProductsController, type: :controller do let(:product) {create(:product)} let(:user) {product.user} let(:category) {product.third_category} describe 'GET #index' do before do get :index end it "renders the :index template" do expect(response).to render_template :index end it "assigns the requested categories to @categories" do create_list(:category, 7) expect(assigns(:categories).size).to eq 3 end it "assigns the requested categories to @brands" do create_list(:brand, 7) expect(assigns(:brands).size).to eq 4 end end describe 'GET #new' do before do login user get :new end it "renders the :new template" do expect(response).to render_template :new end it "assigns @product" do expect(assigns(:product)).to be_a_new Product end end describe 'POST #create' do let(:user) {create(:user)} let(:params){{user_id: user.id, product: attributes_for(:product)}} subject { post:'create', params:params } before do login user get :create end it 'saves the new product in the database' do expect(subject).to change(Product, :count).by(1) end it 'redirects to products#index' do expect(repsonse).to redirect_to products_path end end describe 'GET #show' do context "test the template and an intance variable holding one record" do before do get :show, params: {id: product} end it "renders the :show template" do expect(response).to render_template :show end it "assigns the requested product to @product" do expect(assigns(:product)).to eq product end end context "test @images" do it "only assigns the images of the product with params[:id]" do product2 = create(:product) create_list(:product_image, 2, product: product2) images = create_list(:product_image, 2, product: product) get :show, params: {id: product} expect(assigns(:images)).to eq images end it "can only have up to 4 images" do images = create_list(:product_image, 7, product: product) get :show, params: {id: product} expect(assigns(:images).size).to eq 4 end end context "test @products" do it "does not include the record with the same id as params[:id]" do get :show, params: {id: product} expect(assigns(:products)).not_to include(product) end it "can only have up to 6 records" do create_list(:product, 7, user: user) get :show, params: {id: product} expect(assigns(:products).size).to eq 6 end end context "test @category_products" do it "does not include the record with the same id as params[:id]" do get :show, params: {id: product} expect(assigns(:category_products)).not_to include product end it "can only have up to 6 records" do category_products = create_list(:product, 7, third_category: category) get :show, params: {id: product} expect(assigns(:category_products).size).to eq 6 end end end end

以下のコードはfactroybotです。

FactoryBot.define do factory :product do name {'アメリカンイーグルのTシャツ'} description {'買ったばっかり'} size {'M'} product_status {0} delivery_fee {0} prefecture_id {1} lead_time {0} price {'300'} transaction_status {0} first_category_id {create(:category).id} second_category_id {create(:category).id} third_category_id {create(:category).id} user end end

以下のコードはproduct_controllerの記述です。

class ProductsController < ApplicationController before_action :authenticate_user!, only: [:new, :create] before_action :set_product, only: :show def index @categories = Category.limit(3) @brands = Brand.limit(4) @first_categories = Category.where(ancestry: nil) end def new @product = Product.new @product.product_images.build end def show @images = @product.product_images.limit(4) @products = ProductDecorator.decorate_collection(@product.user.products.where.not(id: params[:id]).limit(6)) @category_products = ProductDecorator.decorate_collection(@product.third_category.third_category_products.where.not(id: params[:id]).limit(6)) if user_signed_in? @like = Like.find_by(user_id: current_user.id, product_id: params[:id]) end @prev_item = @product.showPrevItem if @product.checkPrevItem @next_item = @product.showNextItem if @product.checkNextItem end def create @product = Product.new(product_parameter) respond_to do |format| if @product.save params[:product_images][:image].each do |image| @product_image = @product.product_images.create(image: image, product_id: @product.id) end format.html{redirect_to root_path} else @product.product_images.build format.html{render action: 'new'} end end end private def set_product @product = ProductDecorator.decorate(Product.find(params[:id])) end def product_parameter params.require(:product).permit(:name, :description, :first_category_id, :second_category_id, :third_category_id, :size, :product_status, :delivery_fee, :prefecture_id, :lead_time, :price, :transaction_status, product_images_attributes: [:image]).merge(user_id: current_user.id) end end

どなたかわかる方がいらっしゃいましたらご教授の方よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問