2015-05-29 167 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 

這可能會解決您的問題。

相關問題