1
Rails 4 앱에서 간단한 검색 기능을 만들고 있지만 검색 기능을 사용하여 결과를 표시하지 않습니다.Rails 4의 검색 양식을 사용하여 결과를 찾지 못했습니다.
여러 번 시도한 결과 내 컨트롤러에서 "%#{params[:search]}%"
을 사용하면 항상 아무 것도 반환하지 않습니다. 만약 내가 "example"
같은 일반 문자열을 사용하면 잘 작동합니다. 내 코드에 문제가 있습니까? 감사!
보기
<div class="row">
<%= link_to "new post", new_post_path, :class => "tiny button" %>
<%= form_tag posts_path, method: :get do %>
<%= text_field_tag :search, params[:search], placeholder: "search" %>
<% end %>
</div>
<% @post.each do |post| %>
<h3 class="post_title">
<% end %>
컨트롤러
class PostsController < ApplicationController
def index
if params[:search]
@post = Post.where('title LIKE ?', '%#{params[:search]}%')
else
@post = Post.all
end
end
routes.rb
Rails.application.routes.draw do
resources :posts
root to: "posts#index"
end
모델
class Post < ActiveRecord::Base
validates :title, presence: true
end
에 작은 따옴표 안에 문자열에 보간을 사용할 수 없습니다! 고마워요 !!! –
보간을 사용하는 이유는 무엇입니까? 'Post.where ('LIKE?', params [: search]) ' – rmagnum2002
OP는 이것을 사용하여'% '기호를 추가합니다.이 경우에는 첫 번째 인수로 이동할 수도 있습니다. – Santhosh