搜索表单有两个字段不起作用

时间:2014-10-14 14:28:36

标签: ruby ruby-on-rails-3

我正在制作一个允许用户使用CRUD指南的RoR应用。我还添加了各种搜索功能,包括通过两个属性“投资者”和“程序”进行搜索的选项,这两个属性是我搜索表单中的两个文本字段。它不起作用。我的逻辑在按一个属性搜索时起作用,但不是两个。这几乎可以肯定是因为我不理解如何将两个文本字段作为params发送到搜索功能,而且我似乎找不到足够的文档来自己找到答案。

我的模型文件:

def self.invprogsearch(params[:investor], params[:program])
    where(['investor LIKE? && program LIKE?', "%#{investor}%", "%#{program}%"])
  end

我的索引控制器

def index
   if params[:invprogsearch]
      @guidelines = Guideline.invprogsearch(params[:invprogsearch])
    else
      @guidelines = Guideline.all
    end
  end

和我的搜索表单

<p>Search by Investor and Program</p>
<%= form_tag(guidelines_path, :method => "get", id: "search-form") do %>
  <%= text_field(:guideline, :program_code, placeholder: "Enter investor") %>
  <%= text_field(:guideline, :investor, placeholder: "Enter program") %>
  <%= submit_tag "Search", :name => nil %>
<% end %>

我很欣赏这里的帮助,我渴望更加精通Ruby / Rails

3 个答案:

答案 0 :(得分:0)

尝试重新编写代码:

查看:

<p>Search by Investor and Program</p>
<%= form_tag(guidelines_path, :method => "get", id: "search-form") do %>
  <%= text_field_tag :program_code, nil, placeholder: "Enter investor" %>
  <%= text_field_tag :investor, nil, placeholder: "Enter program" %>
  <%= submit_tag "Search", :name => nil %>
<% end %>

型号:

def self.invprogsearch(params)
    where('investor LIKE ? AND program LIKE ?', "%#{params[:investor]}%", "%#{params[:program_code]}%")
  end

控制器:

def index
   if params[:program_code].present? && params[:investor].present?
      @guidelines = Guideline.invprogsearch(params)
    else
      @guidelines = Guideline.all
    end
  end
end

此外,这是一个关于实现搜索功能的精彩截屏视频: https://www.codeschool.com/screencasts/basecamp-search

答案 1 :(得分:0)

您的模型应该是

def self.invprogsearch(investor, program)
  where(['investor LIKE ? && program LIKE ?', "%#{investor}%", "%#{program}%"])
end

params在您的模型定义被读取时未设置。

控制器应该说

def index
  if params[:invprogsearch]
    @guidelines = Guideline.invprogsearch(params[:investor], params[:program_code])
                                          #     ^-----   Change here
  else
    @guidelines = Guideline.all
  end
end

基本问题是表单中字段的名称需要与您传递到模型中的params中的字段的名称相匹配。

答案 2 :(得分:0)

我希望这会有所帮助。

处理重用范围

是一个好主意
class Guideline < ActiveRecord::Base

    scope :by_program_code, -> (program_code) {
        if program_code.present?
            where(arel_table[:program_code].match("%#{program_code}%"))
        else
            current_scope
        end
    }

     scope :by_investor, -> (investor) {
        if investor.present?
            where(arel_table[:investor].match("%#{investor}%"))
        else
            current_scope
        end
    }


    scope :by_program_code_and_investor -> (program_code, investor) {
        by_program_code(program_code).by_investor(investor)
    }
end

在控制器中

class GuidelinesController < ApplicationController

    before_action :guidelines, only: [:index]

    def index; end

    protected 

    def guidelines
        if params[:program_code] && params[:investor] 
            @guidelines ||= Guideline.by_program_code_and_investor(params[:program_code], params[:investor]) 
        end

         @guidelines ||= Guideline.all
    end

end

如果您想要更灵活的搜索

def guidelines
    @guidelines = Guideline
    @guidelines = @guidelines.by_program_code(params[:program_code]) if params[:program_code]
    @guidelines = @guidelines.by_investor(params[:investor]) if params[:investor]
    @guidelines = @guidelines.all 
end