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

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

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

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

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

0回答

561閲覧

RSpecでStrongParameterで使用しているリスト型のデータ設定がしたい。

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

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

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2021/02/17 14:16

編集2021/02/19 06:17

前提・実現したいこと

下記のエラーメッセージの通りに、テストコードのtag_idsがない為、
パラメータの数がたりないのでエラーが発生していることはわかります。
ですが、リスト型のデータを渡すことができず、同じエラーが発生します。

ググっても解決できなかったので質問いたしました。
お手数をかけ致しますがご理解のある方は、
ヒントでもいいのでお教えをお願い致します。

発生している問題・エラーメッセージ

1) BoardsController get#create 掲示板を新規作成した場合 作成された掲示板のページが表示されること Failure/Error: params.require(:board).permit(:name, :title, :body, tag_ids: []) ActionController::ParameterMissing: param is missing or the value is empty: board # ./app/controllers/boards_controller.rb:54:in `board_params' # ./app/controllers/boards_controller.rb:15:in `create' # /usr/local/bundle/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/template_assertions.rb:62:in `process' # /usr/local/bundle/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/integration.rb:16:in `block (2 levels) in <module:Integration>' # ./spec/controllers/boards_controller_spec.rb:16:in `block (4 levels) in <top (required)>'

該当のソースコード

#####boards_controller_spec.rb[テストコード]

require 'rails_helper' RSpec.describe BoardsController, type: :controller do describe "get#create" do context "掲示板を新規作成した場合" do before do get :create, params: { board_params: { name: "tanaka", title: "Ruby on Rails 5", body: "Hello Rails", tag_ids: ["Ruby on Rails5"], } } end it "作成された掲示板のページが表示されること" do expect(response).to redirect_to board end end end end

#####boards_controller.rb[テスト対象のコントローラー]

class BoardsController < ApplicationController def index @boards = params[:tag_id].present? ? Tag.find(params[:tag_id]).boards : Board.all @boards = @boards.page(params[:page]) end def new @board = Board.new end def create board = Board.new(board_params) if board.save flash[:notice] = "「#{board.title}」の掲示板を作成しました。" redirect_to board else redirect_to new_board_path, flash: { board: board, error_messages: board.errors.full_messages } end end private def board_params # strongparameterで取得 params.require(:board).permit(:name, :title, :body, tag_ids: []) end # 1件分のレコードを取得 def set_target_board @board = Board.find(params[:id]) end end

#####関連モデル
board.rb

# == Schema Information # # Table name: boards # # id :integer not null, primary key # body :text(65535) # name :string(255) # title :string(255) # created_at :datetime not null # updated_at :datetime not null # class Board < ApplicationRecord has_many :comments, dependent: :delete_all has_many :board_tag_relations, dependent: :delete_all has_many :tags, through: :board_tag_relations validates :name, presence: true, length: { maximum: 10 } validates :title, presence: true, length: { maximum: 30 } validates :body, presence: true, length: { maximum: 1000 } end

tag.rb

# == Schema Information # # Table name: tags # # id :integer not null, primary key # name :string(255) not null # created_at :datetime not null # updated_at :datetime not null # class Tag < ApplicationRecord has_many :board_tag_relations, dependent: :delete_all has_many :boards, through: :board_tag_relations end

board_tag_relation.rb

# == Schema Information # # Table name: board_tag_relations # # id :integer not null, primary key # created_at :datetime not null # updated_at :datetime not null # board_id :integer # tag_id :integer # # Indexes # # index_board_tag_relations_on_board_id (board_id) # index_board_tag_relations_on_tag_id (tag_id) # # Foreign Keys # # fk_rails_... (board_id => boards.id) # fk_rails_... (tag_id => tags.id) # class BoardTagRelation < ApplicationRecord belongs_to :board belongs_to :tag end

#####rails routesの結果[boards関連のみ抜粋]

boards GET /boards(.:format) boards#index POST /boards(.:format) boards#create new_board GET /boards/new(.:format) boards#new edit_board GET /boards/:id/edit(.:format) boards#edit board GET /boards/:id(.:format) boards#show PATCH /boards/:id(.:format) boards#update PUT /boards/:id(.:format) boards#update DELETE /boards/:id(.:format) boards#destroy

試したこと

テストコードboards_controller_spec.rbの修正のみ
結果は同じエラーメッセージで失敗でした。

#1 get :create, params: { board_params: { name: "tanaka", title: "Ruby on Rails 5", body: "Hello Rails", tag_ids: "Ruby on Rails5", } } #2 get :create, params: { board_params: { name: "tanaka", title: "Ruby on Rails 5", body: "Hello Rails", tag_ids: { name: "Ruby on Rails5" }, } }

補足情報(FW/ツールのバージョンなど)

[作業環境]
macOS BigSur 11.2
Docker 20.10
Ruby 2.4.5
Rails 5.2.2
RSpec 3.9.0

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問