【问题标题】:Rails - Simple Search from Railscast Not DisplayingRails - Railscast 的简单搜索不显示
【发布时间】:2014-01-05 19:09:46
【问题描述】:

我一直在关注有关如何创建简单搜索表单的 railscast 教程, 出于某种奇怪的原因,每当我搜索 URL 更改并且服务器日志显示它搜索帖子时,在这种情况下是广告,但它不显示它。

我的广告控制器。

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy]


  # GET /ads
  # GET /ads.json
  def index
    @ads = Ad.search(params[:search])
    @ads_small = Ad.where(:size => "small").order('created_at DESC')
    @ads_medium = Ad.where(:size => "medium").order('created_at DESC')
    @ads_featured = Ad.where(:size => "featured").order('created_at DESC')

  end

  # GET /ads/1
  # GET /ads/1.json
  def show
  end

  # GET /ads/new
  def new
    @ad = Ad.new
  end

  # GET /ads/1/edit
  def edit
  end

  # POST /ads
  # POST /ads.json
  def create
    @ad = Ad.new(ad_params)
    @ad.user_id = current_user.id

    respond_to do |format|
      if @ad.save
        format.html { redirect_to @ad, notice: 'Ad was successfully created.' }
        format.json { render action: 'show', status: :created, location: @ad }
      else
        format.html { render action: 'new' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ads/1
  # PATCH/PUT /ads/1.json
  def update
    respond_to do |format|
      if @ad.update(ad_params)
        format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ads/1
  # DELETE /ads/1.json
  def destroy
      @ad.destroy
      respond_to do |format|
        format.html { redirect_to ads_url }
        format.json { head :no_content }
      end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_ad
      @ad = Ad.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def ad_params
      params.require(:ad).permit(:title, :url, :preview, :location, :size, :info, :search)
    end
end

我的广告模型

class Ad < ActiveRecord::Base

    attr_accessible :title, :url, :preview, :size, :location, :info
    belongs_to :user
  has_attached_file :preview, :default_url => "missing.jpg", :styles => { :medium => "125x125^", :featured => "250x250^", :showpg => "400x400^" }, :convert_options => {:medium => "-gravity center -extent 125x125", :featured => "-gravity center -extent 250x250", :showpg => "-gravity center -extent 400x400"}

  validates :title, length: { maximum: 35 }
  validates :url, length: { maximum: 40 }

  def self.search(search)
  if search
    find(:all, :conditions => ['title LIKE ?', "#{search}"])
  else
    find(:all)
  end
end
end

我的广告索引视图

 <%= form_tag ads_path, :method => :get do %>
  <p class="search_f">
    <%= text_field_tag :search, params[:search], :class => "glow_f" %>
    <%= submit_tag "Search", :name => nil, :class => "submit" %>
<% end %>

<% @ads.each do |ad| %>
<% if ad.size == "featured" %>
  <div class="adspace_grid_f">
    <div class="pic_sec_f">
      <%= image_tag ad.preview.url(:featured) %>
    </div>
    <div class="text_info_f">
      <span class="bold"><%= link_to ad.title, ad, :class => "title" %></span></br>
      <p><%= truncate(ad.info, length: 90) %></p>
      <span class="bold"><%= ad.location %></span></br>
      <span class="bold"><%= ad.url %></span>

    </div>
    <% if can? :destroy, ad %>
      <%= link_to 'Delete', ad, :method => :delete, :class => "delete" %> 
    <% end %>
  </div> 
<% end %>
<% if ad.size == "medium" %>
  <div class="adspace_grid">
    <div class="pic_sec">
      <%= image_tag ad.preview.url(:medium) %>
    </div>
    <div class="text_info">
      <span class="bold"><%= link_to ad.title, ad, :class => "title" %></span></br>
      <p><%= truncate(ad.info, length: 60) %></p>
      <span class="bold"><%= ad.location %></span></br>
      <span class="bold"><%= ad.url %></span>

    </div>
    <% if can? :destroy, ad %>
      <%= link_to 'Delete', ad, :method => :delete, :class => "delete" %> 
    <% end %>
  </div> 
<% end %>
<% end %>

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails forms search rails-activerecord railscasts


    【解决方案1】:

    检查ad.size 以获取符合您搜索条件的广告。如果大小不是“特色”或“中等”,您的视图似乎会忽略它,因为唯一的 if 条件适用于 ad.size == "featured"ad.size == "medium"

    如果您想查看搜索结果,请将此行添加到 &lt;% @ads.each do |ad| %&gt; 行上方:

    <% @ads.inspect %>
    

    此外,@ads_small@ads_medium@ads_featured 似乎并未在任何地方使用。这些是为了未来的目的吗?

    【讨论】:

    • 啊,好吧,我的搜索似乎是大写敏感度的问题,无论如何你知道删除大写敏感度吗?
    • 哦,@ads_small/medium/featured 确实出现在我的代码中,我上面发布的视图只是其中的一个。
    • 如果您使用的是 PostgreSQL,请在您的 search 方法中使用 ILIKE 而不是 LIKE
    • 如果您想在数据中的任何位置搜索该值,您可以使用%,类似['title ILIKE ?', "%#{search}%"]