[英]Rails 4 Search Engine: Multiple Params
I'm fairly new to rails and at the moment coding my first app. 我是Rails的新手,目前正在编写我的第一个应用程序。
I created a search for the usernames of my users, but since then I've added a tagging system (using 'acts_as_taggable_on' gem), which I now want to implement in the search functionality. 我为用户的用户名创建了一个搜索,但是从那时起,我添加了一个标记系统(使用'acts_as_taggable_on'gem),现在我想在搜索功能中实现该系统。
I've tried it several times now, but always got an Argument error (either 0 for 1 or 2 for 1). 我已经尝试了几次,但是总是收到Argument错误(0代表1或2代表1)。
That is how you target the user objects with the tag params: 这就是使用标签params定位用户对象的方式:
# Find users with any of the specified tags:
User.tagged_with(["awesome", "cool"], :any => true, :wild => true)
User Model 用户模型
acts_as_ordered_taggable
# Search
def self.search(search)
if search
where(["username LIKE ?", "%#{search}%"])
else
all
end
end
# Search Tags
def self.searchtags(searchtags)
if searchtags
tagged_with(["#{searchtags}"], :any => true, :wild => true)
else
none
end
end
Users Controller 用户控制器
def index
@users = User.search(params[:search])
@tagged_users = User.searchtags(params[:searchtags]
end
User Index View 用户索引视图
= form_tag users_path, :method => 'get' do
= text_field_tag :search, params[:search]
= submit_tag "Search"
= form_tag users_path, :method => 'get' do
= text_field_tag :searchtags, params[:searchtags]
= submit_tag "Search Tags"
- @users.each do |user|
= image_tag gravatar_for user if user.use_gravatar == true
= image_tag user.avatar_filename.url if user.use_gravatar == false
%h2= link_to user.username, user
%p= link_to "Favorite", userfavorite_user_path(user, type: "favorite"), method: :get
%p= link_to "Unfavorite", userfavorite_user_path(user, type: "unfavorite"), method: :get
%p= user.tag_list
- @tagged_users.each do |user|
= image_tag gravatar_for user if user.use_gravatar == true
= image_tag user.avatar_filename.url if user.use_gravatar == false
%h2= link_to user.username, user
%p= link_to "Favorite", userfavorite_user_path(user, type: "favorite"), method: :get
%p= link_to "Unfavorite", userfavorite_user_path(user, type: "unfavorite"), method: :get
%p= user.tag_list
Hope someone can help me by finding a solution, thanks. 希望有人能找到解决方案来帮助我,谢谢。
I am not quite sure, but I've seen the same problems here before. 我不太确定,但是我以前在这里也遇到过同样的问题。 The reason was incomatiability of the versions of 'acts_as_taggable_on' gem with the rails version. 原因是'acts_as_taggable_on'gem的版本与rails版本不兼容。 I wanted to add this as a comment to your post, but unfortunately, it is still not permitted for my account. 我想将其添加为您的帖子的评论,但是很遗憾,我的帐户仍然不允许这样做。 You may find solutions here agged_with(params[:skill]) ArgumentError: wrong number of arguments (given 2, expected 1) 您可能会在这里找到解决方案agged_with(params [:skill])ArgumentError:参数数量错误(给定2个,预期为1个)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.