前提・実現したいこと
nurseriesの一覧から、
title(施設名)・city(エリア)の絞り込み検索を実装したく、
以下のサイトを参考にして実装を進めてみています。
https://blog.codecamp.jp/rails_text_21
発生している問題・エラーメッセージ
modelにscopeを使って絞り込みを定義しようとしたところ、
以下のようなエラーメッセージが出てしまいます。
対処方法について、アドバイスいただけます方がいらっしゃいましたら、何卒よろしくお願いたします。
undefined method `scope' for main:Object
該当のソースコード
<nursery.rb>
class Nursery < ApplicationRecord enum city: { "足立区": 0, "荒川区": 1, "板橋区": 2 } end # 施設名による絞り込み scope :get_by_title, ->(title) { where("title like ?", "%#{title}%") } # エリアよる絞り込み scope :get_by_city, ->(city) { where(city: city) }
<nurseries.controller>
class NurseriesController < ApplicationController def index @nurseries = Nursery.page(params[:page]).per(20) if params[:title].present? @nurseries = @nurseries.get_by_title params[:title] end if params[:city].present? @nurseries = @nurseries.get_by_city params[:city] end end
<index.html.erb>
<div> <%= form_tag(nurseries_path, method: "get") do %> <%= label_tag :name, "施設名" %> <%= text_field_tag :name, params[:title] %> <%= label_tag :name, "エリア" %> <%= select_tag :city, options_for_select(Nursery.cities), include_blank: true %> <%= submit_tag "検索" %> <% end %> </div> <% @nurseries.each do|nursery| %> 施設名<%= link_to nursery.title, nursery_path(nursery.id), method: :get%></br> 住所<%= nursery.prefecture %><%= nursery.city %><%= nursery.street %><%= nursery.building %></br> 最寄駅<%= nursery.station %></br> 電話番号<%= nursery.tel %></br> 定員数<%= nursery.capacity %></br> <% end %> <%= paginate(@nurseries) %>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/22 01:23
2019/01/22 01:24