下記サイトを参考にマイクロポスト検索機能を実装したのですがRails test時にエラーが発生しました
参考URL:https://yukitoku-sw.hatenablog.com/entry/2019/11/10/001418
↓エラー内容
test_micropost_interface#MicropostsInterfaceTest (1.63s) ActionView::Template::Error: ActionView::Template::Error: No Ransack::Search object was provided to search_form_for! app/views/shared/_microposts_search.html.erb:3:in `_app_views_shared__microposts_search_html_erb___4334420189547037187_49418860' app/views/static_pages/home.html.erb:16:in `_app_views_static_pages_home_html_erb___1981986679562933160_58706460' app/controllers/microposts_controller.rb:12:in `create' test/integration/microposts_interface_test.rb:15:in `block (2 levels) in <class:MicropostsInterfaceTest>' test/integration/microposts_interface_test.rb:14:in `block in <class:MicropostsInterfaceTest>'
どの箇所を修正すればよいのかわからず困っております、ご存じの方がいらっしゃったら教えていただけると幸いです。
_microposts_search.html
HTML
1<div class="row"> 2 <div class="search_form"> 3 <%= search_form_for @q, url: @url do |f| %> 4 <%= f.label :content_cont, 'Micropost Search'%> 5 <div class="input-group"> 6 <%= f.search_field :content_cont, placeholder: "Enter keyword..", class: 'form-control' %> 7 <span class="input-group-btn"> 8 <%= f.submit 'Search', class: "btn btn-primary" %> 9 </span> 10 </div> 11 <% end %> 12 </div> 13</div>
home.html.erb
HTML
1<% if logged_in? %> 2 <div class="row"> 3 <aside class="col-md-4"> 4 <section class="user_info"> 5 <%= render 'shared/user_info' %> 6 </section> 7 <section class="stats"> 8 <%= render 'shared/stats' %> 9 </section> 10 <section class="micropost_form"> 11 <%= render 'shared/micropost_form' %> 12 </section> 13 </aside> 14 <div class="col-md-8"> 15 <h3>Timeline</h3> 16 <%= render 'shared/microposts_search' %> 17 <%= render 'shared/feed' %> 18 </div> 19 </div> 20<% else %> 21<div class="center jumbotron"> 22 <h1>Welcome to the My first App</h1> 23 24 <h2> 25 This is the home page for the 26 <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a> 27 sample application. 28 </h2> 29 30 <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %> 31</div> 32 33<% end %>
microposts_controller
Ruby
1class MicropostsController < ApplicationController 2 before_action :logged_in_user, only: [:create, :destroy] 3 before_action :correct_user, only: :destroy 4 5 def create 6 @micropost = current_user.microposts.build(micropost_params) 7 if @micropost.save 8 flash[:success] = "Micropost created!" 9 redirect_to root_url 10 else 11 @feed_items = [] 12 render 'static_pages/home' 13 end 14 end 15 16 def destroy 17 @micropost.destroy 18 flash[:success] = "Micropost deleted" 19 redirect_to request.referrer || root_url 20 end 21 22 private 23 24 def micropost_params 25 params.require(:micropost).permit(:content, :picture) 26 end 27 28 def correct_user 29 @micropost = current_user.microposts.find_by(id: params[:id]) 30 redirect_to root_url if @micropost.nil? 31 end 32end
microposts_interface_test.rb
Ruby
1require 'test_helper' 2 3class MicropostsInterfaceTest < ActionDispatch::IntegrationTest 4 5 def setup 6 @user = users(:michael) 7 end 8 9 test "micropost interface" do 10 log_in_as(@user) 11 get root_path 12 # assert_select 'div.pagination' div classにpagination追加するとGreenになるけどレイアウト崩れるので無効 13 # 無効な送信 14 assert_no_difference 'Micropost.count' do 15 post microposts_path, params: { micropost: { content: "" } } 16 end 17 assert_select 'div#error_explanation' 18 # 有効な送信 19 content = "This micropost really ties the room together" 20 assert_difference 'Micropost.count', 1 do 21 post microposts_path, params: { micropost: { content: content } } 22 end 23 assert_redirected_to root_url 24 follow_redirect! 25 assert_match content, response.body 26 # 投稿を削除する 27 assert_select 'a', text: 'delete' 28 first_micropost = @user.microposts.paginate(page: 1).first 29 assert_difference 'Micropost.count', -1 do 30 delete micropost_path(first_micropost) 31 end 32 # 違うユーザーのプロフィールにアクセス (削除リンクがないことを確認) 33 get user_path(users(:archer)) 34 assert_select 'a', text: 'delete', count: 0 35 end 36end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/03 00:26
2020/11/03 01:18
2020/11/03 02:24
2020/11/03 02:26