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

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

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

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

2367閲覧

【Ruby on rails】undefined method `start_time_qteq' for Ransack::Search の解決方法

Tomoaki_Fukuda

総合スコア75

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/10/29 07:05

###前提・実現したいこと
Ruby on railsにてイベント一覧が表示されるシステムを作っています。
そのイベントを検索する機能を実装するべく、gemの「Ransack」を導入致しました。
さらにイベントの表示項目の中に「開催時間(start_time)」があるのですが、その検索機能の実装で、下記のエラーメッセージが表示されてしまい困っております。
是非ともアドバイス頂きたくお願い申し上げます。

###発生している問題・エラーメッセージ
NoMethodError in Home#top
Showing /Users/TOMOAKI/Gokon/app/views/home/top.html.erb where line #12 raised:

undefined method `start_time_qteq' for Ransack::Search<class: Event, base: Grouping <combinator: and>>:Ransack::Search
Extracted source (around line #12):

<%= f.label :start_time_qteq, '開催日' %>

<div>

<%= f.date_select :start_time_qteq, prompt:true %>

</div> </div>

<%= f.submit '検索', class: 'byn btn-default' %>

###こちらもご確認ください。
https://gyazo.com/888e5d354bfe8c8a0501f005f79983c5

###top.html.erb

ruby

1<% if user_signed_in? %> 2 <aside class="sidebar"> 3 <%= search_form_for @q do |f| %> 4 5 <div class="form-group"> 6 <%= f.label :name_cont, 'イベント名' %> 7 <%= f.text_field :name_cont, class: 'form-control' %> 8 </div> 9 <div class ="form-group"> 10 <%= f.label :start_time_qteq, '開催日' %> 11 <div> 12 <%= f.date_select :start_time_qteq, prompt:true %> 13 </div> 14 </div> 15 <%= f.submit '検索', class: 'byn btn-default' %> 16 <% end %> 17 18 </aside> 19 20 <ul class="notes"> 21 <h2>イベントの一覧</h2> 22 <%= render @events %> 23 <%= paginate @events %> 24 </ul> 25 26 27<% else %> 28 <div class="top-wrapper"> 29 <%= image_tag "gatebook_cover.png" %> 30 <div class="register-wrapper"> 31 <h1><%= @message %></h1> 32 <%= link_to "新規登録", new_user_registration_path, class: "btn btn-large register-btn" %> 33 </div> 34 </div> 35<% end %>

###home_controller.rb

ruby

1class HomeController < ApplicationController 2 3PER = 3 4 5def top 6 if user_signed_in? 7 @note = Note.new 8 @notes = Note.all.order(created_at: :desc) 9 #@event = Event.new 10 #@events = Event.all.order(created_at: :desc) 11 @event = Event.new 12 @events = Event.all.order(created_at: :desc).page(params[:page]).per(PER) 13 @user = User.new 14 @q = Event.ransack(params[:q]) 15 @events = @q.result(distinct: true) 16 else 17 @message = "ようこそプロトタイプサイトへ!" 18 end 19 end 20 21def index 22@q = Event.ransack(params[:q]) 23@events = @q.result(distinct: true) 24end 25 26private 27 28def search_params 29 params.require(:q).permit! 30 resocue 31 { start_time_qteq: Time.zone.now } 32 end 33 34 def about 35 end 36 37 include ApplicationHelper 38end

###events_controller.rb

ruby

1class EventsController < ApplicationController 2 before_action :authenticate_user! 3 before_action :correct_user, only: [:edit, :update] 4 before_action :set_event, only: [:show, :edit, :update, :destroy] 5 include ApplicationHelper 6 7 def show 8 end 9 10 def index 11 @events = Event.all 12 end 13 14 def new 15 event = Event.new 16 end 17 18 def create 19 @event = current_user.events.build(event_params) 20 if @event.save 21 redirect_to @event, notice: "投稿が保存されました" 22 else 23 # @notesを定義してください 24 @events = Event.all.order(created_at: :desc) 25 # renderメソッドで表示するビューが、views/home/top.html.erbになるように変更してください 26 render 'home/top' 27 28 end 29 end 30 31 def edit 32 end 33 34 def update 35 file = params[:event][:image] 36 @event.set_eventimage(file) 37 38 if @event.update(event_params) 39 redirect_to @event, notice: 'ユーザー情報が更新されました' 40 else 41 render :edit 42 end 43 end

###event.rb

ruby

1class Event < ActiveRecord::Base 2 3validates :user_id, presence: true 4validates :name, presence: true 5validates :place, presence: true, length: { maximum: 100 } 6validates :content, presence: true, length: { maximum: 2000 } 7validate :start_time 8validate :end_time 9validate :start_time_should_be_before_end_time 10 11belongs_to :user 12 13paginates_per 2 14 15private 16 17def start_time_should_be_before_end_time 18 return unless start_time && end_time 19 20 if start_time >= end_time 21 errors.add('開始時間は終了時間よりも前に設定してください') 22 end 23end 24 25def set_eventimage(file) 26 if !file.nil? 27 file_name = file.original_filename 28 File.open("public/event_images/#{file_name}", 'wb'){|f| f.write(file.read)} 29 self.image = file_name 30 end 31end 32 33 34end

###Gemfile

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' end

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

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

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

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

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

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

gouf

2016/10/29 11:01

start_time_qteq とありますが、 gteq のtypo ではありませんか?
Tomoaki_Fukuda

2016/10/30 04:51

上記ご指摘の通りでした。ありがとうございました。
guest

回答1

0

ベストアンサー

start_time_qteqとありますが、やりたいことは"以上"で検索したいと思うので、英語で言うとgreater than or equalとなりまうす。その場合はransackでは_qteqではなくgteqとします。
ですので、start_time_qteqとなっている箇所をstart_time_gteqのように修正することでエラーにならなくなると思います。

投稿2016/10/29 12:34

cameluby

総合スコア891

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

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

Tomoaki_Fukuda

2016/10/30 04:50

丁寧な解説誠にありがとうございました。 上記アドバイスの通り,start_time_gteqに修正したところ、無事に解決することが出来ました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問