在我的ruby on rails应用程序中,我在索引页面上有一个下拉菜单,用户可以在其中更改显示的结果量,例如从6个结果更改为3个结果。当应用程序加载时,它在index.html.erb页面上工作,但是当使用搜索并返回相关结果时,会更改每页显示的结果,我会收到以下错误:
ProductsController#multi_find中的TypeError 无法将nil转换为String
Extracted source (around line #64):
62 def self.multi_find(cat_id, search)
63 search_price = search
64 search_condition = "%" + search + "%"
65 # test if cat_id is not blank
66 if not cat_id.blank?
67 # assign the value of cat_id to a ‘scope’ named :cat
这是index.html.erb文件中的下拉菜单:
<div id="per-page">
<form>
Results per page:
<select name="per">
<option value="3">3</option>
<option value="6">6</option>
<option value="9">9</option>
</select>
<button>
Go
</button>
</form>
</div>
这是products_controller,其中每页的结果被添加到索引中:
def index
@products = Product.order(:title).page(params[:page]).per(params.fetch(:per, '6').to_i)
end
这是products_controller中的multi_find方法,使用下拉菜单中断:
def multi_find
# call a Product class method, using two parameters; a category unique identifier and a search string (author or title)
products = Product.multi_find(params[:cat_id], params[:search_string])
@products = Kaminari.paginate_array(products.order :title).page(params[:page]).per(params.fetch(:per, '6').to_i)
# if no products have been found
if products.empty?
# display a notice
flash.now[:alert] = "No records found - displaying all records ..."
# then display all products
@products = Product.order(:title).page(params[:page]).per(params.fetch(:per, '6').to_i)
end
# use the index view
render :action => "index"
end
product.rb multi_find方法:
def self.multi_find(cat_id, search)
search_price = search
search_condition = "%" + search + "%"
# test if cat_id is not blank
if not cat_id.blank?
# assign the value of cat_id to a ‘scope’ named :cat
scope :cat, -> { where('category_id = ?', cat_id)}
# using the ‘scope’ cat find where a search string is like a title or an author’s name
self.cat.where("title LIKE ? OR author_name LIKE ? OR price = ?", search_condition, search_condition, search_price)
# else
else
# find where a search string is like a title or an author’s name
self.cat.where("title LIKE ? OR author_name LIKE ? OR price = ?", search_condition, search_condition, search_price)
# end
end
如果我完全删除了:
.per(params.fetch(:per, '6').to_i)
只需使用.per(6)它就可以正常工作,只有在替换它时才尝试允许用户通过multi_find搜索来选择每页的数量。
有人可以帮忙吗?提前谢谢。
答案 0 :(得分:0)
您收到错误是因为search
位于此处的最后一行:
def self.multi_find(cat_id, search)
search_price = search
search_condition = "%" + search + "%"
...等于零。这意味着当你这样做时:
Product.multi_find(params[:cat_id], params[:search_string])
... params[:search_string]
必须为零。您没有发布名称属性等于&#39; search_string&#39;的任何html。
然而,如果这是真的:
如果我完全删除了:
.per(params.fetch(:per, '6').to_i)
只需使用.per(6)就可以了, 只有在更换它时才尝试允许用户选择 使用multi_find搜索的每页数量。
然后,不要将如此多的代码塞进一行,而是在将参数值粘贴到.per()之前对其进行排序:
period_from_select = params[:per]
puts "*****period_from_select=#{period_from_select}" #look in the server window for this output
if period_from_select
period_from_select = period_from_select.to_i
else
period_from_select = 6
end
puts "****period_from_select=#{period_from_select}"
...per(period_from_select)