2015-05-29 168 views
1

我有一个简单的搜索表单,在rails 4应用程序,需要两个参数传递到能够显示相关数据。ActiveRecord :: StatementInvalid在显示控制器

我收到'Mysql2 ::错误:未知列'数据输入',但列确实存在。如果我代替'@search = Page.where(params[:one] && params[:two])',则使用'@search = Page.all'数据显示,但全部显示。

<%= form_tag(page_show_path, id: "search-form") do %> 
    <%= text_field_tag :one, params[:one], placeholder: "One" %> 
    <%= text_field_tag :two, params[:two], placeholder: "Two" %> 
    <%= submit_tag "Search", :name => nil %> 
<% end %> 

模型

def self.one(query) 
    where("one = ?", "%#{query}%") 
    end 
    def self.two(query) 
    where("two = ?", "%#{query}%") 
    end 

控制器

def show 

    if (params[:one] && params[:two]).present? 
    @search = Page.where(params[:one] && params[:two]) 
    else 
    redirect_to page_path, notice: "Not a valid combination" 
    end 
    end 
+0

什么是您的列名? –

+0

一个和两个..... – DollarChills

+0

尝试用'一个'来改变'one'。可能是这个问题是大小写敏感的。 –

回答

2

您可以创建和使用范围。

scope :find_one_two, ->(query_one, query_two) { where("one = ? AND two = ? ", query_one, query_two) } 

@search = Page.find_one_two(params[:one], params[:two]) 

OR

可以使用。

@search = Page.where("one = ? AND two = ?", params[:one], params[:two]) 
+0

这就得到了它,使用第二个选项。我发誓我尝试过,虽然......哈哈。啤酒时间! – DollarChills

0
def show 
    if (params[:one] && params[:two]).present? 
    @search = Page.where("one like ? AND two like ? ", "%#{params[:one]}%", "%#{params[:two]}%") 
    else 
    redirect_to page_path, notice: "Not a valid combination" 
    end 
end 

这可能会解决您的问题。

相关问题