gpt4 book ai didi

ruby-on-rails - 语法错误 : (irb):26: both block arg and actual block given

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:54 25 4
gpt4 key购买 nike

我有这个问题

= f.select(:city, Country.where(:country_code => "es").collect(&:cities) {|p| [ p.city, p.id ] }, {:include_blank => 'Choose your city'})

问题是我收到以下错误

SyntaxError: (irb):26: both block arg and actual block given

据我所知,我通过包含 collect(&:cities) 然后声明该 block 做错了什么。有没有一种方法可以用同一个查询来完成这两个任务?

最佳答案

Country.where(:country_code => "es").collect(&:cities)

完全一样

Country.where(:country_code => "es").collect {|country| country.cities}

这就是您收到错误的原因:您将两个 block 传递给 collect 方法。你的实际意思可能是这样的:

Country.where(:country_code => "es").collect(&:cities).flatten.collect {|p| [ p.city, p.id ] }

这将检索国家/地区,获取每个国家/地区的城市列表,将数组扁平化为只有一维的数组,然后返回您的数组以供选择。

由于每个国家代码可能只有一个国家,你也可以这样写:

Country.where(:country_code => "es").first.cities.collect {|p| [ p.city, p.id ] }

关于ruby-on-rails - 语法错误 : (irb):26: both block arg and actual block given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com