2度目の質問です。現在Ruby on Rails で簡単な単語登録アプリを制作しています。
単語一覧のindex.html.hamlに単語検索機能を実装中ですが、下記のエラーが出ていて困っております。ご教授のほど、何卒宜しく、お願い致します。
#エラー文
NoMethodError in Words#index Showing /Users/hayashiryouware/projects/words/app/views/words/index.html.haml where line #6 raised: undefined method `to_key' for #<Word::ActiveRecord_Associations_CollectionProxy:0x00007ffd9c8d24e8> Did you mean? to_set to_ary Extracted source (around line #6): 4 = link_to '単語登録', new_group_word_path, class:'btn-register' 5 .kensaku_word 6 = form_for @words, mehod: :get, url:group_words_path do |f| 7 = f.text_field :keyword, placeholder:'投稿を検索する', class:'serch_field' 8 = f.submit "検索", class:"search" 9 - @words.each do |word|
#ソースコード
#app> models> word.rb
class Word < ApplicationRecord belongs_to :user belongs_to :group mount_uploader :image, ImageUploader def self.search(search) return Word.all unless search Word.where('word LIKE(?)', "%#{search}%") end end
#config> routes.rb
Rails.application.routes.draw do devise_for :users root to: 'homes#index' resources :groups, only: [:index, :new, :create, :edit, :update, :destroy] do resources :words, only: [:index, :new, :create, :edit, :update, :destroy] collection do get 'search' end end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
#app> controllers> words_controller.rb
class WordsController < ApplicationController #before_action :move_to_index, except: [:index, :search] def index @group = Group.find(params[:group_id]) @words = @group.words end def new @words = Word.new end def create @words = Word.create(word_params) redirect_to group_words_path end def edit @word = Word.find(params[:id]) end def update word = Word.find(params[:id]) word.update(word_params) redirect_to group_words_path end def destroy word = Word.find(params[:id]) word.destroy redirect_to group_words_path end def search @word = Word.new @words = Word.search(params[:keyword]) end private def word_params params.require(:word).permit(:word, :meaning, :sentence, :image).merge(user_id: current_user.id, group_id: params[:group_id]) end end
#app> views> words> index.html.haml
.serach_word = form_with mehod: :get, url:group_words_path do |f| = f.text_field :keyword, placeholder:'投稿を検索する', class:'serch_field' = f.submit "検索", class:"search" - @words.each do |word| .folk .center .textblock .word = word.word .meaning = word.meaning .image = image_tag word.image.url if word.image? .btnblock .edit_word = link_to '編集', edit_group_word_path(id:word.id), class:"edit_word_word" .delete_word = link_to '削除', group_word_path(id:word.id), method: :delete, class:"delete_word_word" .sentence = word.sentence .check =check_box_tag :check1 =label_tag :check1, 'Who' =check_box_tag :check2 =label_tag :check2, 'When' =check_box_tag :check3 =label_tag :check3, 'Where' =check_box_tag :check4 =label_tag :check4, 'What' =check_box_tag :check5 =label_tag :check5, 'Why' =check_box_tag :check6 =label_tag :check6, 'How'
#app> views> words> search.html.haml
.serach_word = form_with mehod: :get, url:group_words_path do |f| = f.text_field :keyword, placeholder:'投稿を検索する', class:'serch_field' = f.submit "検索", class:"search" - @words.each do |word| .folk .center .textblock .word = word.word .meaning = word.meaning .image = image_tag word.image.url if word.image? .btnblock .edit_word = link_to '編集', edit_group_word_path(id:word.id), class:"edit_word_word" .delete_word = link_to '削除', group_word_path(id:word.id), method: :delete, class:"delete_word_word" .sentence = word.sentence .check =check_box_tag :check1 =label_tag :check1, 'Who' =check_box_tag :check2 =label_tag :check2, 'When' =check_box_tag :check3 =label_tag :check3, 'Where' =check_box_tag :check4 =label_tag :check4, 'What' =check_box_tag :check5 =label_tag :check5, 'Why' =check_box_tag :check6 =label_tag :check6, 'How'
#調べた内容・考え
form with を使用するとビューの表示は出来るのですが、検索ボタンをクリックしても検索されない為、form forで実装を試みた所、上記のエラーに当たってしまいました。 正直初めてみるエラーで検索してもしっくりくる記事がなく、質問しました。