我有以下developers_controller
,我想执行基于location_params
的查询。Rails循环变量
在index_update
操作中,我创建了一个变量location
,其中location_params
作为用户给出的研究输入。我想对此变量执行each
循环,以基于用户请求过滤来自@locations = Location.all
的数据。
这是我的私有方法location_params
:
def location_params
params.require(:location).permit(:country, :location, :surfspot, :barbecue, :villa, :swimmingpool, :skiresort, :singleroom)
end
这是我index_update
行动:
def index_update
location = Location.new(location_params)
locations = Location.all
location.each do |param, value|
if value != nil
locations = locations.where(param => value)
end
end
end
我不能找出如何循环的location
变量。
这是我的Rails控制台我的位置模型:
=> #<Location:0x000000036b6838
id: nil,
host_id: nil,
description: nil,
location: nil,
singleroom: nil,
sharedroom: nil,
surfspot: nil,
barbecue: nil,
villa: nil,
swimmingpool: nil,
skiresort: nil,
created_at: nil,
updated_at: nil,
country: nil,
state: nil,
houseimages: nil
什么是您的实际问题?循环或div的宽度? – bork
而不是'if value!= nil'使用'if!value.nil?'。 –
难道你不应该循环'位置',而不是'位置'? –