Error Message: How to Resolve No Ransack::Search object was provided to search_form_for!

Asked 2 years ago, Updated 2 years ago, 60 views

Prerequisites/What you want to achieve

We are creating a system where Ruby on rails displays a list of events.
"In order to implement the function of searching for the event, we are trying to introduce the ""Ransack"" of the gem, but at that time, the following error message was displayed, which is troubling me."
Please give me some advice.

Problems/Error Messages you are experiencing

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@qdo|f|%>

  <div class="form-group">
    <%=f.label:name_cont, 'Event Name'%>

Also, please check this as well.
https://gyazo.com/c9374b17b436d0b09524d91e09bb8d5a

The following code is provided.


top.html.erb

<%if user_signed_in?%>
  <aside class="sidebar">
  <%=search_form_for@qdo|f|%>

  <div class="form-group">
    <%=f.label:name_cont, 'Event Name'%>
    <%=f.text_field:name_cont,class:'form-control'%>
  </div>
  <div class="form-group">
    <%=f.label: start_time_qteq, 'date' %>
    <div>
      <%=f.date_select:start_time_qteq, prompt:true%>
    </div>
  </div>
  <%=f.submit 'search', class: 'byn btn-default'%>
  <%end%>

  </aside>

    <ul class="notes">
    <h2>List of events</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 Registration", new_user_registration_path, class: "btn btn-large register-btn" %>
    </div>
  </div>
<%end%>

home_controller.rb

class HomeController <ApplicationController

PER = 3

deftop
    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])
      # @[email protected](distinct:true)
    else
      @message="Welcome to the prototype site!"
    end
  end

def index
@q = Event.search (params[:q])
@[email protected](distinct:true)
end

private

def search_params
  param.require(:q).permit!
  resocue
  { start_time_qteq:Time.zone.now}
  end

  about
  end

  include ApplicationHelper
end

events_controller.rb

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)
    [email protected]
      redirect_to @event, notice: "Post Saved"
    else
      # Please define @notes
      @ events = Event.all.order (created_at::desc)
      # Change the view displayed by the render method to views/home/top.html.erb
      render 'home/top'

    end
  end

  default
  end

 default update
    file=params[:event][:image]
    @event.set_eventimage(file)

    [email protected](event_params)
      redirect_to @event, notice: 'User information updated'
    else
      render —edit
    end
 end

event.rb

class Event <ActiveRecord::Base
  values:user_id, presence:true
  values:name, presence:true
  values:place,presence:true,length:{maximum:100}
  values:content, presence:true, length:{maximum:2000}
  validate —start_time
  validate —end_time
  validate —start_time_should_be_before_end_time

  belongs_to —user

  paginates_per2

  private

  def start_time_should_be_before_end_time
    return unless start_time&end_time

    if start_time>=end_time
      errors.add('Start time must be set before end time')
    end
  end

  default_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

Gemfile

source 'https://rubygems.org'
 # Bundle edge Rails installed: 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 assemblies
   gem 'uglifer', '>=1.3.0'
 # Use CoffeeScript for.coffee assemblies and views
   gem 'coffee-rails', '~>4.1.0'
  # See https://github.com/rails/execjs#readme for more supported runtime
 # 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 execrake doc —rails generate 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 'device', '3.5.1'
gem'kaminari'
gem 'bootstrap-ass', '3.2.0.0'
gem 'ransack', '~>1.2.2'
end

If you need any other information, please let me know.
Thank you for your cooperation.

ruby-on-rails ruby

2022-09-30 20:11

1 Answers

I'm not confident because I don't use Ransack, but

Since home/top has an error, it is necessary to enter @q in top in the controller.

class HomeController <ApplicationController

  deftop
    @q = Event.search (params[:q])
    @[email protected](distinct:true)
    #...

  end
end

How about


2022-09-30 20:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.