Rails 7 中带有参数中断的路由路径

问题描述 投票:0回答:1

我正在将现有的 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

但问题依然存在。应该做什么?

完整的错误信息:

#、@path=#>、@memo=nil、@right=#>、@memo=nil、@right=#>、@memo=nil、@right=# 的未定义方法 `map' 、@regexp=/[^./?]+/、@name=“格式”>>、@memo=nil>>>、@path_params=[:格式]、@names=[“格式”]、@符号=[#、@regexp=/[^./?]+/、@name="format">]、@stars=[]、@terminals=[#>、#>、#>、 #、@regexp=/[^./?]+/、@name="format">]、@wildcard_options={:controller=>"recording_sets"、:action=>"index"}>、@spec= #>、@memo=nil、@right=#>、@memo=nil、@right=#>、@memo=nil、@right=#、@regexp=/[^./?]+ /、@name="format">>、@memo=nil>>>、@requirements={}、@separators="/.?"、@anchored=true、@names=["format"]、@optional_names =[“格式”]、@required_names=[]、@re=nil、@offsets=nil、@requirements_for_missing_keys_check={}>、@request_method_match=[ActionDispatch::Journey::Route::VerbMatchers::GET]、@约束={}, @defaults={:controller=>"recording_sets", :action=>"index"}, @required_defaults=nil, @_required_defaults=[:controller, :action], @required_parts=[], @parts =[:format]、@precedence=60、@path_formatter=#>]、@children=[]、@parameters=[1]>]、@children=[2]、@parameters=[]>、@scope_options ={}、@internal=nil、@source_location="config/routes.rb:55"、@ast=#>、@memo=nil、@right=#>、@memo=nil、@right=# >、@memo=nil、@right=#、@regexp=/[^./?]+/、@name="format">>、@memo=nil>>>>、@parameterized_parts={} ,@params={:kind=>1}>

  super(route_key, options, recall).map(&:dup)
                                   ^^^^

你是说吗?点击

ruby-on-rails routes
1个回答
0
投票

仔细查看 Gemfile.lock,我发现某些 Gems 有多个版本,特别是 actionpack(这是错误消息的来源)。我清除了 Gemfile 中的所有版本限制并重新捆绑。这解决了这个神秘的问题

© www.soinside.com 2019 - 2024. All rights reserved.