我有以下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?'。 –
難道你不應該循環'位置',而不是'位置'? –