前提・実現したいこと
※勉強中の初心者になります。
CRUDで作成した簡単なwebアプリに検索機能を追加。
検索画面(index)と検索結果を分けて表示したい。
※同一の画面内で、検索画面と検索結果を表示させることはできました。
これを分けたいのが実現したい内容になります。
現状、分けることは成功しましたが、
検索結果側で何も表示されず。。
発生している問題・エラーメッセージ
検索画面(index)と検索結果(search)をわけるにあたり、
ルーティングを書いて、検索画面(index)から検索後、
検索結果(search)側の画面で何も表示されず。。
URLをみると下記の記載がありました。
・URLの抜粋
us-east-2.amazonaws.com/customers/search.%23Customer::ActiveRecord_Relation:0x007fa9745b1420
該当のソースコード
・ルーティングの結果
ec2-user:/environment/customer-db (master) $ rails routes/environment/customer-db (master) $
Prefix Verb URI Pattern Controller#Action
search_customers GET /customers/search(.:format) customers#search
customers GET /customers(.:format) customers#index
POST /customers(.:format) customers#create
new_customer GET /customers/new(.:format) customers#new
edit_customer GET /customers/:id/edit(.:format) customers#edit
customer GET /customers/:id(.:format) customers#show
PATCH /customers/:id(.:format) customers#update
PUT /customers/:id(.:format) customers#update
DELETE /customers/:id(.:format) customers#destroy
ec2-user:
・index.html.erb ※検索画面
こちらが検索画面になります。
※下記がコードです
・search.html.erb ※検索結果
こちらが検索結果を表示する画面になります。
※下記がコードです
<% end %>
・customers_controller.rb
こちらがコントローラーの内容で、
ここの書き方もわかってない状況です。。
class CustomersController < ApplicationController
def index end def search @customers = Customer.search(params[:search]) end
・customer.rb
class Customer < ApplicationRecord
def self.search(search)
if search
Customer.where(['company LIKE ?', "%#{search}%"])
else
'#Customer.all
end
end
end
試したこと
indexページを表示し、検索ワードを入れて検索すると画面遷移はするものの、
検索結果側でデータが表示されず。
コントローラのindexに値を追記したりしてみましたが変わらず。。
補足情報(FW/ツールのバージョンなど)
rails 5.0.7
AWS cloud9環境
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。