简体   繁体   中英

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!

try this:

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

When you say elsif searchtags it is actually trying to call the method defined as self.searchtags . Since that method requires a searchtags argument to be passed in, you are seeing the error.

I'm not sure what you are trying to check so I don't know the correct way to fix it, but I'm guessing that you need to make the searchtags argument optional by defining it as self.searchtags(searchtags = nil)

Also, you'll need to use elsif searchtags.present? since none returns a Relation object.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM