2013-12-17 7 views
0

URL 변경 사항을 검색 할 때마다 간단한 검색 양식을 만드는 방법에 대한 railscast 자습서 ( )를 따라 왔으며 서버 로그가 이 경우에는 광고를 게재하지만 게재되지는 않습니다.레일스 - Railscast에서 간단한 검색이 표시되지 않음

내 광고 컨트롤러.

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 %> 

는 어떤 도움을 크게 감상 할 수있다.

답변

1

ad.size에서 검색 가능한 광고를 확인하십시오. 크기가 "추천"또는 "보통"이 아닌 경우보기가및 ad.size == "medium"의 유일한 if 조건이므로 무시됩니다.

<% @ads.inspect %> 
또한

, @ads_small, @ads_medium@ads_featured 어디서나 사용할 수 있도록 표시되지 않습니다 :

당신은 검색에 의해 발견되는 것을보고 싶다면

는 이것을 <% @ads.each do |ad| %> 라인 위의 라인을 포함한다. 그것들은 미래의 목적을위한 것입니까?

+0

아, 알았습니다. 검색시 자본 민감도를 제거하는 데 문제가있는 것으로 보입니다. – coreypizzle

+0

아, 그리고 @ ads_small/medium/featured는 내 코드에 나타납니다. 위에 게시 한 뷰는 단지 코드 조각입니다. – coreypizzle

+0

PostgreSQL을 사용하는 경우'search' 메소드에서'LIKE' 대신'ILIKE'을 사용하십시오. – cschroed