【发布时间】:2015-08-05 19:36:52
【问题描述】:
我最近将我的 Rails 应用程序从 4.0 更新到了 4.1。一切似乎都工作正常,除了我的 Resource_Tag 模型中的这一行之前工作。
基本上,我想按标签名称和 District_Resource 名称搜索/查找 District_Resources。
**ex.**
If I search the word "Tutoring"
*I should get all District_Resources with the Resource_Tag "Tutoring"
*And all District Resources that include the word Tutoring in it's Name.
(i.e Tutoring Services)
由于某种原因,我不断收到此错误:
wrong number of arguments (1 for 0)
all(:conditions => (string ? [cond_text, *cond_values] : []))
控制器
class ResourceTagsController < ApplicationController
def index
if params[:search].present?
#Calls Search Model Method
@resource_tags = ResourceTag.search(params[:search])
@tagsearch = ResourceTag.search(params[:search])
@tag_counts = ResourceTag.count(:group => :name,
:order => 'count_all DESC', :limit => 100)
else
@resource_tags = ResourceTag.all
end
end
end
模型
class DistrictResource < ActiveRecord::Base
has_many :district_mappings, dependent: :destroy
has_many :resource_tags, through: :district_mappings
accepts_nested_attributes_for :resource_tags
end
class ResourceTag < ActiveRecord::Base
#create relationships with all resource and mapping models
has_many :district_mappings, dependent: :destroy
has_many :district_resources, through: :district_mappings
#I GET AN ERROR HERE
def self.search(string)
return [] if string.blank?
cond_text = string.split(', ').map{|w| "name like ?"}.join(" OR ")
cond_values = string.split(', ').map{|w| "%#{w}%"}
all(:conditions => (string ? [cond_text, *cond_values] : []))
end
end
观看次数
<%= form_tag(resource_tags_path, :method => 'get', class: "navbar-search") do %>
<form>
<%= text_field_tag :search, params[:search], :class => "search-query form-control" %>
<%= submit_tag "Search", :name => nil, :class => "search-button" %>
</form>
<% end %>
【问题讨论】:
-
不足以回答,但在 Rails 4 中,
all(conditions)可能会被弃用。不管你是否应该使用where(attr1: value, attr2: value) -
您能告诉我发生此错误的
params[:search]的值吗? -
"搜索"=>"指导、辅导"
标签: ruby search ruby-on-rails-4.1 mysql2