我试图创建一个简单的搜索功能并获取wrong number of arguments (2 for 0..1)
错误。我跟着每一个简单的搜索栏教程,看看我可能做错了什么,没有运气调试。使用ActiveResource时的简单搜索
另外要注意(这可能是此错误的原因),我使用ActiveResource从我的工作模型的API中提取记录。
这是我有:
# 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 %>
错误跟踪:
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
你也可以添加错误堆栈吗?这将帮助我找出哪些线路出现错误。 –
@ArupRakshit这够了吗? – mikeymurph77
是的,它足以知道它从哪里来。 –