为了给你一个简短的答案,你可以马上开始,你可以这样做:
if search == 'Primary'
data, search = RainFallSearcher.call(rain_fall_type, compare)
end
class RainFallSearcher
def call(rain_fall_type, compare)
if rain_fall_type == "Agriculture, Forestry and Fishing"
if compare == "None"
search = "None"
rain_fall_type
elsif compare == "All"
rain_fall_type = compare
data = [
"Crops",
"Livestock",
"Forestry and Logging",
"Fishing and Aquaculture",
]
else
search = "None"
rain_fall_type = compare
end
elsif rain_fall_type == "None"
data = [
"Primary",
]
else
data = [
"Agriculture, Forestry and Fishing",
"Mining and Quarrying",
]
end
return data, search
end
end
RainFallSearcher
在一个由Rails加载的位置(我会选择
app/services
您不必将所有逻辑都放在控制器或模型中,实际上建议将它们精简,并创建简单(或复杂)的Ruby对象来处理逻辑。
这篇文章很有解释性:
https://www.engineyard.com/blog/keeping-your-rails-controllers-dry-with-services