I am trying to create a simple search feature and getting a wrong number of arguments (2 for 0..1)
error. I've followed every simple search rails tutorial to see what I might be doing wrong and had no luck debugging.
Also to note (which might be the reason for this error), I am using ActiveResource to pull records from an API for my Job model.
This is what I have:
# jobs_controller.rb
def index
if params[:search]
#ActiveResource does not have an .order which led me to use the code below.
@jobs = Job.search(params[:search]).sort_by(&:posted).reverse
else
@jobs = Job.find(:all).sort_by(&:posted).reverse
end
end
# job.rb
def self.search(search)
where("city LIKE ?", "%#{search}")
end
# views/jobs/index.html.erb
<%= form_tag(jobs_path, :method => "get", id: "search-form") do %>
<%= search_field_tag :search, params[:search], placeholder: "Search Jobs" %>
<%= submit_tag "Search", name: nil %>
<% end %>
<% if @jobs.present? %>
<% @jobs.each do |job| %>
<div>
<%= link_to job.title, job_path(job) %>
</div>
<% end %>
<% else %>
There are no posts containing the term(s) <%= params[:search] %>.
<% end %>
Error Trace:
Processing by JobsController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"Fort Worth"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
ArgumentError (wrong number of arguments (2 for 0..1)):
app/models/job.rb:16:in `search'
app/controllers/jobs_controller.rb:4:in `index'
wrong number of arguments (2 for 0..1)
Extracted source (around line #16):
15 def self.search(search)
16 where("city LIKE ?", "%#{search}")
17 end
18 end