params[:search] 更新到 Rails 4.1 后出错
params[:search] error after updating to Rails 4.1
我最近将我的 Rails 应用程序从 4.0 更新到 4.1。一切似乎都工作正常,除了我的 Resource_Tag 模型中的这一行之前工作。
基本上,我想 search/find District_Resources 标签名称和 District_Resource 名称。
**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.1 之后的 all
中,ActiveRecord
的方法不接受任何参数,这就是为什么会出现额外参数错误的原因。您可以尝试 where
代替。这是您的搜索方法
def search(string)
return [] if string.blank?
cond_text = string.split(', ').map{|w| "name like ?"}.join(" OR ")
cond_values = string.split(', ').map{|w| "%#{w}%"}
self.where(string ? [cond_text, *cond_values] : [])
end
我最近将我的 Rails 应用程序从 4.0 更新到 4.1。一切似乎都工作正常,除了我的 Resource_Tag 模型中的这一行之前工作。
基本上,我想 search/find District_Resources 标签名称和 District_Resource 名称。
**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.1 之后的 all
中,ActiveRecord
的方法不接受任何参数,这就是为什么会出现额外参数错误的原因。您可以尝试 where
代替。这是您的搜索方法
def search(string)
return [] if string.blank?
cond_text = string.split(', ').map{|w| "name like ?"}.join(" OR ")
cond_values = string.split(', ').map{|w| "%#{w}%"}
self.where(string ? [cond_text, *cond_values] : [])
end