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

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

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

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

2回答

308閲覧

indexビューに検索機能を実装したいです。

ryongon

総合スコア5

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/03/02 04:25

編集2020/03/02 05:08

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で実装を試みた所、上記のエラーに当たってしまいました。 正直初めてみるエラーで検索してもしっくりくる記事がなく、質問しました。

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

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

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

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

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

hatsu

2020/03/02 04:37

Showing /Users/hayashiryouware/projects/words/app/views/words/index.html.haml where line #6 raised: の6行目と書かれていると思うのですが、6行目に該当する行も含めて載っていますか? = link_to '単語登録', new_group_word_path, class:'btn-register' ようなコードが書いてあるらしいのですが、ご質問内容のコードには含まれていなそうで。。
ryongon

2020/03/02 05:09

ありがとうございます。 申し訳ございません。 エラー文を修正いたしました。 宜しくお願い致します。
guest

回答2

0

VIEWの6行目の『mehod』は『method』ではないでしょうか。

投稿2020/03/02 08:59

makox0105

総合スコア65

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

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

0

index.html.hamlのform_withの後ろが"mehod"になっていませんか?

あとその下の行のtext_fieldのclassも"serch_field"ですがsearch_fieldの方がよいのでは?
余計なお世話だったらすみません(・ω・;)

投稿2020/03/02 05:54

編集2020/03/02 06:13
ryuuzu

総合スコア30

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問