2016-02-01 91 views
2

我正在學習rails通過教程進行高級搜索,其中用戶可以根據類別找到帖子。但不知何故,我的高級頁面給我這個錯誤:Rails 4 ArgumentError - 參數數量錯誤

ArgumentError in Searches#new Showing app/views/searches/new.html.haml where line #10 raised:

wrong number of arguments (3 for 1..2)

上,我得到的錯誤是該行:

= s.text_field :category, options_for_select(@categories), :include_blank => true 

上述文件是new.html.haml(10號線)。這裏是搜索controller.rb, 我認爲new方法中的@categories是不正確的。

class SearchesController < ApplicationController 

    def new 
     @search = Search.new 
     @categories = Idea.uniq.pluck(:category) 
    end 

    def create 
     @search = Search.create(search_params) 
     redirect_to @search 
    end 

    def show 
     @search = Search.find(params[:idea_id]) 
    end 

    private 

    def search_params 
     params.require(:search).permit(:keywords, :category) 
    end 

end 

而且我model.rb

class Search < ActiveRecord::Base 

    def search_ideas 
     ideas = Idea.all 

     ideas = ideas.where(["title LIKE ?","%#{keywords}%"]) if keywords.present? 
     ideas = ideas.where(["category LIKE ?","%#{category}%"]) if category.present? 

     return ideas 
    end 

end 

我試圖用collection_select,而不是s.text_field但它仍然給了我這個錯誤。

回答

2

嘗試使用s.select這樣的:

= s.select :category, options_for_select(@categories), :include_blank => true 
1

讓指documentation第一。

select(object, method, choices = nil, options = {}, html_options = {}, &block)

因爲它有兩個哈希選項,所以你需要指定optionshtml_options

s.select :category, options_for_select(@categories), {}, {include_blank: true} 
相關問題