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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

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

Q&A

解決済

1回答

740閲覧

ActiveHashを用いた際のsystemspecのエラー

denisov

総合スコア6

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

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

0グッド

0クリップ

投稿2021/04/07 10:48

編集2021/04/07 11:40

□解決したいこと

投稿画面(app/views/tips/new.html.erb)にてsystemspecを行なった際、投稿に成功するとindex.html.erbに移動する段階で以下のエラーが発生しました。

当該エラーを解決したいのですが、ご協力いただけないでしょうか。

以下、エラー本文

ruby

1Failure/Error: <p class="tiped-title-contents">カテゴリー : <%= Category.data[f.category_id][:name] %></p> 2 3 ActionView::Template::Error: 4 undefined method `[]' for nil:NilClass 5 6 [Screenshot]: /Users/taniguroarata/originalapp/archtips/tmp/screenshots/failures_r_spec_example_groups_nested_nested_visit_new_user_session_path_231.png 7 8 9 # ./app/views/tips/_main_area.html.erb:17:in `block in _app_views_tips__main_area_html_erb__1894181271776908624_70358077282820' 10 # ./app/views/tips/_main_area.html.erb:12:in `_app_views_tips__main_area_html_erb__1894181271776908624_70358077282820' 11 # ./app/views/tips/index.html.erb:10:in `_app_views_tips_index_html_erb___4391342711310590138_70357992921920' 12 # ------------------ 13 # --- Caused by: --- 14 # NoMethodError: 15 # undefined method `[]' for nil:NilClass 16 # ./app/views/tips/_main_area.html.erb:17:in `block in _app_views_tips__main_area_html_erb__1894181271776908624_70358077282820'

spec/system/tips_spec.rb

ruby

1require 'rails_helper' 2 3RSpec.describe "投稿する", type: :system do 4 before do 5 @tip = FactoryBot.create(:tip) 6 @user = FactoryBot.create(:user) 7 end 8 9 context '投稿に成功した時' do 10 it 'visit new_user_session_path' do 11 visit new_user_session_path 12 fill_in 'user_email', with: @user.email 13 fill_in 'user_password', with: @user.password 14 find('input[type="submit"]').click 15 expect(current_path).to eq(root_path) 16 expect(page).to have_content('新規投稿') 17 visit new_tip_path 18 fill_in 'tip_title', with: @tip.title 19 select '法規・条例', from: 'tip_category_id' #ここが問題と思われます 20 fill_in 'tip_description', with: @tip.description 21 binding.pry 22 expect{ 23 find('input[type="submit"]').click 24 }.to change {Tip.count }.by(1) 25 expect(current_path).to eq(root_path) 26 end 27 end 28end

app/views/tips/new.html.erb (テスト時の画面)

※画像の投稿は必須ではないため、今回は飛ばしてテストしております

ruby

1<div class = "wrapper"> 2 <%= render "shared/header" %> 3 <div class="main-tips-new"> 4 <div class = "new-tip-string"> 5 <p>新規投稿</p> 6 </div> 7 <%= form_with(model: @tip, local: true) do |form| %> 8 <%= render 'layouts/error_messages', model: form.object, class:"" %> 9 <div class="tip-title"> 10 <p>タイトル</p> 11 <div class="tip-title-content"> 12 <%= form.text_field :title, placeholder: "" %> 13 </div> 14 </div> 15 <div class="tip-category"> 16 <p>カテゴリー</p> 17 <div class="tip-category-content"> 18 <%= form.collection_select(:category_id, Category.all, :id, :name, {}, {class:""}) %> 19 </div> 20 </div> 21 <div class="tip-images"> 22 <div class="tip-image-main"> 23 <p class="tip-image-main-text">画像(メイン)</p> 24 <div id="image-list"> 25 <%= form.file_field :image, class: 'tip-image-main-img' %> 26 </div> 27 </div> 28 <div class="tip-image-sub"> 29 <p class="tip-image-sub-text">画像(サブ)</p> 30 <div class="tip-image-sub-images"> 31 <%= form.file_field :image, class: 'tip-image-sub-img' %> 32 <%= form.file_field :image, class: 'tip-image-sub-img' %> 33 <%= form.file_field :image, class: 'tip-image-sub-img' %> 34 </div> 35 </div> 36 </div> 37 <div class="tip-description"> 38 <p class="tip-description-text">説明</p> 39 <%= form.text_field :description, placeholder:"", class: 'text-form' %> 40 </div> 41 <%= form.submit "投稿する" %> 42 <% end %> 43 </div> 44</div>

app/views/tips/_main_area.html.erb

ruby

1<div class="main-top"> 2 <div class="new-tip-title"> 3 <p>最新の投稿</p> 4 </div> 5 <div class="search-area"> 6 <div> 7 <input class="search-form"placeholder= " キーワードを入力"> 8 </div> 9 <input value="検索" class="search-button"> 10 </div> 11</div> 12<% @tips.each do |f| %> 13 <div class="tiped"> 14 <div class="tiped-description"> 15 <h3 class="tiped-title"><%= f.title %></h3> 16 <p class="tiped-title-contents">投稿時間 : <%= l f.created_at%></p> 17 <p class="tiped-title-contents">カテゴリー : <%= Category.data[f.category_id][:name] %></p> 18 <p class="tiped-title-contents">投稿者 : <%= f.user.nickname%></p> 19 <p class="tiped-title-contents">説明 : <%= f.description%></p> 20 </div> 21 <div class="tiped-image-field"> 22 <div class="main-image", id="main-image"> 23 <%= image_tag f.image, class: 'uped-image-main' if f.image.attached? %> 24 </div> 25 </div> 26 </div> 27<% end %>

app/models/category.rb

ruby

1class Category < ActiveHash::Base 2 self.data = [ 3 { id: 1, name: '--' }, 4 { id: 2, name: '法規・条例' }, 5 { id: 3, name: '納まり・詳細図' }, 6 { id: 4, name: '仕上げ' }, 7 ] 8 9 include ActiveHash::Associations 10 has_many :categories 11 12 end

環境

'rails', '~> 6.0.0'

'capybara', '>= 2.15'

ruby '2.6.5'

□仮説

new.html.erbにてテストを行なっている際、

「カテゴリー」はActiveHashを用いて、選択するしようとなっております。

spec/system/tips_spec.rb では 

ruby

1select '法規・条例', from: 'tip_category_id'

と記述しており、ここが問題と想定しております。

new.html.erbで投稿が成功したら、index.html.erbからrenderした app/views/tips/_main_area.html.erb で投稿内容が表示されます。

その際、カテゴリーはidを引数に表示を行なっております。

spec/system/tips_spec.rb でselect '法規・条例'としていることでidが選択されないため、このようなエラーが発生しているものと考えました。

□仮説を元に実行したこと

systemspecでidを選択する必要があると思い

ruby

1fill_in 'tip_category_id', with: @tip.category_id

としましたが、当該記述ではカテゴリーが選択されませんでした。

おそらく、記述形式ではないためfill_inは有効ではないということと想定されます。

spec/system/tips_spec.rb

ruby

1select '法規・条例', from: 'tip_category_id'

部分の適切な記述方法等ございましたらご教示いただけないでしょうか。

ご協力いただきたくお願い申し上げます。

□結果

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

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

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

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

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

dalance

2021/04/07 11:37

タグがRubyではなくRustになっていますので、修正して頂いたほうが良いかと思います。
denisov

2021/04/07 11:40

ありがとうございます。 タグの修正させていただきました。
guest

回答1

0

自己解決

select '法規・条例', from: 'tip_category_id'

当該部分を

select Category.data[@tip.category_id][:name], from: 'tip_category_id'

とすることで解決しました。
原因は仮説の通り、indexでの表示をcategory_idの番号を引数にしておりましたので、
当該引数をもとに、Categoryモデルから'法規・条例'を選択し、指定することで解決しました。

お騒がせをして申し訳ございませんでした。

投稿2021/04/08 21:57

denisov

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問