Gugubaight Gugubaight - 2 months ago 8
Ruby Question

Rails 4 Error: ArgumentError - wrong number of arguments

I am pretty new to rails and currently coding my first application. I created a search to search through my Users (either usernames or tags - using acts_as_taggable_on gem). Following error appears:

ArgumentError in UsersController#index
wrong number of arguments (0 for 1)


User Model

# Search
def self.search(search)
if search
where(["username LIKE ?", "%#{search}%"])
elsif searchtags
none
else
all
end
end
# Search Tags
def self.searchtags(searchtags)
if searchtags
tagged_with(["#{searchtags}"], :any => true, :wild => true)
else
none
end
end


User 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


IMPORTANT!


The error only appears with this piece of code:

elsif searchtags
none


in the User Model, without it everything works fine. I need it though to prevent duplications.


Thanks in advance for your help!

Ptr Ptr
Answer

Do you need to make it possible to perform two searches at once? If not I would use something like this:

Model:

# Search 
def self.search(search=nil)
    # if search is nil this will return all users
    where(["username LIKE ?", "%#{search}%"])
end
# Search Tags
def self.searchtags(searchtags)
    tagged_with(["#{searchtags}"], :any => true, :wild => true)
end

Controller:

def index
  if params.has_key?(:searchtags)
     @users = User.searchtags(params[:searchtags])
  else
     @users = User.search(params[:search])
  end
end

Remove @tagged_users.each loop from view.

Comments