indexで検索機能を実装中(この検索機能はtitle,などを検索するとその検索結果に引っかかったもののみ出るというものにしたいです)
検索機能でエラーが出てしまいます
No route matches {:action=>"show", :controller=>"information"}, missing required keys: [:id]
https://qiita.com/yusuko/items/cff4e46aeafbc3beecf2
このサイトを参考に検索機能を作っていたのですが、
index.html.erb
ruby
1 <% content_for :header do %> 2<section class="hero is-warning"> 3 <div class="hero-body"> 4 <div class="container"> 5 <h1 class="title"> 6 Browse the latest test information 7 </h1> 8 </div> 9 </div> 10</section> 11 12<%= form_tag information_path, :method => 'get' do %> 13 <p> 14 <%= text_field_tag :search, params[:search] %> 15 <%= submit_tag "Search", :name => nil %> 16 </p> 17<% end %> 18<% end %> 19 20<div class="instrument-index-grid pt4"> 21 <% @informations.each do |information| %> 22 23 <div class="instrument border-light"> 24 <div class="instrument-thumb"> 25 <%= link_to image_tag(information.image_url(:thumb)), information %> 26 27 <% if information.condition? %> 28 <div class="condition"> 29 <span class="tag is-dark"><%= information.condition %></span> 30 </div> 31 <% end %> 32 </div> 33 34 35 <div class="pa3"> 36 37 <h3 class="fw7 f4 title"><%= link_to information.title, information %></h3> 38 39 <p class="has-text-gray fg pt1">Post by <%#= information.user.name %></p> 40 41 42 <% if information_author(information) %> 43 <%= link_to 'Edit', edit_information_path(information), class: "button is-small" %> 44 <%= link_to 'Delete', information, method: :delete, data: { confirm: "Are you sure ?" }, class: "button is-small" %> 45 <% end %> 46 47 </div> 48 </div> 49 <% end %> 50</div>
information_controller
ruby
1 class InformationController < ApplicationController 2 before_action :set_information, only: [:show, :edit, :update, :destroy] 3 before_action :authenticate_user!, except: [:index, :show] 4 # GET /information 5 # GET /information.json 6 def index 7 @informations = Information.search(params[:search]) 8 end 9 10 # GET /information/1 11 # GET /information/1.json 12 def show 13 end 14 15 # GET /information/new 16 def new 17 @information = current_user.informations.build 18 end 19 20 # GET /information/1/edit 21 def edit 22 end 23 24 # POST /information 25 # POST /information.json 26 def create 27 @information = current_user.informations.build(information_params) 28 29 respond_to do |format| 30 if @information.save 31 format.html { redirect_to @information, notice: 'Information was successfully created.' } 32 format.json { render :show, status: :created, location: @information } 33 else 34 format.html { render :new } 35 format.json { render json: @information.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /information/1 41 # PATCH/PUT /information/1.json 42 def update 43 respond_to do |format| 44 if @information.update(information_params) 45 format.html { redirect_to @information, notice: 'Information was successfully updated.' } 46 format.json { render :show, status: :ok, location: @information } 47 else 48 format.html { render :edit } 49 format.json { render json: @information.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /information/1 55 # DELETE /information/1.json 56 def destroy 57 @information.destroy 58 respond_to do |format| 59 format.html { redirect_to information_index_url, notice: 'Information was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 64 private 65 # Use callbacks to share common setup or constraints between actions. 66 def set_information 67 @information = Information.find(params[:id]) 68 end 69 70 # Never trust parameters from the scary internet, only allow the white list through. 71 def information_params 72 params.require(:information).permit(:condition, :title, :description, :image) 73 end 74end
information.rb
ruby
1def self.search(search) #ここでのself.はUser.を意味する 2 if search 3 where(['name LIKE ?', "%#{search}%"]) #検索とnameの部分一致を表示。User.は省略 4 else 5 all #全て表示。User.は省略 6 end 7 end
idがないとなっているのですが、どこでidを渡せばいいのかわかりません
showではidは渡してあります
回答お願いします!
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/14 08:28