我正在将现有的 Rails 5 应用程序升级到 Rails 7,但在 link_to() 中使用的路径遇到问题。
例如:在我的routes.rb中,我有以下记录集声明:
resources :recording_sets do
member do
get 'export'
end
collection do
get 'about'
end
end
在 Rails 5 中,我可以编写向路径添加临时参数,如下所示:
link_to('Recordings', recording_sets_path(:kind=>1))
在控制器中我会选择这个参数,如下所示:
def index
if params[:kind]
@recording_sets = RecordingSet.where('kind = ?', params[:kind]).where(visibility_limitation)
@kind = params[:kind]
else
@recording_sets = RecordingSet.all.where(visibility_limitation)
@kind = false
end
respond_to do |format|
if @kind.to_s == "1" # Record
format.html { render action: "index_records" }
else
format.html
end
format.json { render json: @recording_sets }
end
end
Rails 7 中出现这种情况,并显示无用的错误消息:
# 的未定义方法“map”
我尝试以两种不同的方式在routes.rb中添加:kind参数的规范:
在资源声明中:
resources :recording_sets, param: :kind do
member do
get 'export'
end
collection do
get 'about'
end
end
并且作为 get 的具体路径:
get 'recording_sets/:kind/', to: 'recording_sets#index'
铁路路线显示了这一点:
GET /recording_sets/:kind(.:format) recording_sets#index
但问题依然存在。应该做什么?
完整的错误信息:
#
super(route_key, options, recall).map(&:dup)
^^^^
你是说吗?点击
仔细查看 Gemfile.lock,我发现某些 Gems 有多个版本,特别是 actionpack(这是错误消息的来源)。我清除了 Gemfile 中的所有版本限制并重新捆绑。这解决了这个神秘的问题