

**前提・実現したいこと**

Ruby on railsにてイベント一覧が表示されるシステムを作っています。  
そのイベントを検索する機能を実装するべく、gemの「Ransack」を導入しようとしておりますが、その際に下記のエラーメッセージが表示されてしまい困っております。  
是非ともアドバイス頂きたくお願い申し上げます。

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

[code]

        ArgumentError in Home#top
    No Ransack::Search object was provided to search_form_for!
    
    
    Extracted source (around line #3):
                  <% if user_signed_in? %>
      <aside class="sidebar">
      <%= search_form_for @q do |f| %>
    
      <div class="form-group">
        <%= f.label :name_cont, 'イベント名' %>
    
[/code]

なお、こちらも合わせてご確認下さい。  
<https://gyazo.com/c9374b17b436d0b09524d91e09bb8d5a>

**以下コード記載致します。**

  
**top.html.erb**

[code]

        <% if user_signed_in? %>
      <aside class="sidebar">
      <%= search_form_for @q do |f| %>
    
      <div class="form-group">
        <%= f.label :name_cont, 'イベント名' %>
        <%= f.text_field :name_cont, class: 'form-control' %>
      </div>
      <div class ="form-group">
        <%= f.label :start_time_qteq, '開催日' %>
        <div>
          <%= f.date_select :start_time_qteq, prompt:true %>
        </div>
      </div>
      <%= f.submit '検索', class: 'byn btn-default' %>
      <% end %>
    
      </aside>
    
        <ul class="notes">
        <h2>イベントの一覧</h2>
       <%= render @events %>
       <%= paginate @events %> 
      </ul>
    
    
    <% else %>
      <div class="top-wrapper">
        <%= image_tag "gatebook_cover.png" %>
        <div class="register-wrapper">
          <h1><%= @message %></h1>
          <%= link_to "新規登録", new_user_registration_path, class: "btn btn-large register-btn" %>
        </div>
      </div>
    <% end %>
    
[/code]

**home_controller.rb**

[code]

    class HomeController < ApplicationController
    
    PER = 3
    
    def top
        if user_signed_in?
          @note = Note.new
          @notes = Note.all.order(created_at: :desc)
          #@event = Event.new
          #@events = Event.all.order(created_at: :desc)
          @event = Event.new
          @events = Event.all.order(created_at: :desc).page(params[:page]).per(PER)
          @user = User.new
          #@q = Event.ransack(params[:q])
          #@events = @q.result(distinct: true)
        else
          @message = "ようこそプロトタイプサイトへ！"
        end
      end
    
    def index
    @q = Event.search(params[:q])
    @events = @q.result(distinct: true)
    end
    
    private
    
    def search_params
      params.require(:q).permit!
      resocue
      { start_time_qteq: Time.zone.now }
      end
    
      def about
      end
    
      include ApplicationHelper
    end
    
[/code]

**events_controller.rb**

[code]

    class EventsController < ApplicationController
      before_action :authenticate_user!
      before_action :correct_user, only: [:edit, :update]
      before_action :set_event, only: [:show, :edit, :update, :destroy]
      include ApplicationHelper
    
      def show
      end
    
      def index
        @events = Event.all
      end
    
      def new
       event = Event.new
      end
    
      def create
        @event = current_user.events.build(event_params)
        if @event.save
          redirect_to @event, notice: "投稿が保存されました"
        else
          # @notesを定義してください
          @events = Event.all.order(created_at: :desc)
          # renderメソッドで表示するビューが、views/home/top.html.erbになるように変更してください
          render 'home/top'
    
        end
      end
    
      def edit
      end
    
     def update
        file = params[:event][:image]
        @event.set_eventimage(file)
    
        if @event.update(event_params)
          redirect_to @event, notice: 'ユーザー情報が更新されました'
        else
          render :edit
        end
     end
    
[/code]

**event.rb**

[code]

    class Event < ActiveRecord::Base
      validates :user_id, presence: true
      validates :name, presence: true
      validates :place, presence: true, length: { maximum: 100 }
      validates :content, presence: true, length: { maximum: 2000 }
      validate :start_time
      validate :end_time
      validate :start_time_should_be_before_end_time
    
      belongs_to :user
    
      paginates_per 2
    
      private
    
      def start_time_should_be_before_end_time
        return unless start_time && end_time
    
        if start_time >= end_time
          errors.add('開始時間は終了時間よりも前に設定してください')
        end
      end
    
      def set_eventimage(file)
          if !file.nil?
            file_name = file.original_filename
            File.open("public/event_images/#{file_name}", 'wb'){|f| f.write(file.read)}
            self.image = file_name
          end
      end
    
    
    end
    
[/code]

**Gemfile**

[code]

    source 'https://rubygems.org'
     # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
       gem 'rails', '4.2.5'
     # Use sqlite3 as the database for Active Record
       gem 'sqlite3'
     # Use SCSS for stylesheets
       gem 'sass-rails', '~> 5.0'
     # Use Uglifier as compressor for JavaScript assets
       gem 'uglifier', '>= 1.3.0'
     # Use CoffeeScript for .coffee assets and views
       gem 'coffee-rails', '~> 4.1.0'
      # See https://github.com/rails/execjs#readme for more supported runtimes
     # gem 'therubyracer', platforms: :ruby
     # Use jquery as the JavaScript library
       gem 'jquery-rails'
      # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
    gem 'turbolinks'
     # Build JSON APIs with ease. Read more:        https://github.com/rails/jbuilder
     gem 'jbuilder', '~> 2.0'
     # bundle exec rake doc:rails generates the API under doc/api.
     gem 'sdoc', '~> 0.4.0', group: :doc
     # Use ActiveModel has_secure_password
     # gem 'bcrypt', '~> 3.1.7'
     # Use Unicorn as the app server
     # gem 'unicorn'
      # Use Capistrano for deployment
      # gem 'capistrano-rails', group: :development
        group :development, :test do
       # Call 'byebug' anywhere in the code to stop execution and get a debugger console
       gem 'byebug'
       end
       group :development do
       # Access an IRB console on exception pages or by using <%= console %> in views
      gem 'web-console', '~> 2.0'
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
    source 'http://rubygems.org'
    gem 'hirb'
    gem 'devise', '3.5.1'
    gem 'kaminari'
    gem 'bootstrap-sass', '3.2.0.0'
    gem 'ransack', '~> 1.2.2'
    end
    
[/code]

**その他必要な情報があれば、ご教示頂きたくお願い申し上げます。**  
何卒よろしくお願いします。


CC BY-SA 3.0
2016-10-17T07:03:35.353
29625
2016-10-21T19:02:14.997
2016-10-18T03:53:20.013
76
13449
question
ruby-on-rails ruby
エラーメッセージ：No Ransack::Search object was provided to search_form_for!の解決方法


`Ransack`を使っていないので自信はないのですが、

`home/top`でエラーが発生してるので Controller内でも `top`で`@q`に値を入れておく必要があるのではないでしょうか。

[code]

    class HomeController < ApplicationController
    
      def top
        @q = Event.search(params[:q])
        @events = @q.result(distinct: true)
        #...
    
      end
    end
    
[/code]

でどうでしょうか。


CC BY-SA 3.0
2016-10-21T19:02:14.997
29786
2016-10-21T19:02:14.997
3271
29625
answer