【问题标题】:Rails 5: rails-jquery-autocomplete routes blocked by page routesRails 5:rails-jquery-autocomplete 路由被页面路由阻塞
【发布时间】:2018-03-09 14:49:10
【问题描述】:

我在 Rails 5 中使用 rails-jquery-autocomplete gem (https://github.com/bigtunacan/rails-jquery-autocomplete) 来让我的数据库搜索自动完成。不幸的是,我的列表模型已经存在的路线似乎干扰了我现在尝试建立的路线。

listings_controller.rb:

autocomplete :listing, :community, full:true, :extra_data => [:subdivision]

routes.rb:

get '/listings/:listing_id/:address/:mls', to: 'listings#show', as: 'listing'

post 'listings/:listing_id', to: 'clients#textme', as: 'new_text_client'

resources :listings do
  get :autocomplete_listing_community, :on => :collection
end

通过resources: :listings do 传递所有三个路由得到:

ArgumentError: Invalid route name, already in use: 'listings' 
You may have defined two routes with the same name using the `:as` option, or 
you may be overriding a route already defined by a resource with the same 
naming. For the latter, you can restrict the routes created with `resources` 
as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-
routes-created

当我运行rake routes 时。但是,如图所示执行此操作:

ArgumentError: Invalid route name, already in use: 'listings' 
You may have defined two routes with the same name using the `:as` option, or 
you may be overriding a route already defined by a resource with the same 
naming. For the latter, you can restrict the routes created with `resources` 
as explained here: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created

我如何让这些都一起工作?

【问题讨论】:

    标签: jquery ruby-on-rails ruby autocomplete routes


    【解决方案1】:

    您可以将except 用于resources 然后它可以在没有这个的情况下工作

    get '/listings/:listing_id/:address/:mls', to: 'listings#show', as: 'listing'
    
    resources :listings, except: [:show] do
        get :autocomplete_listing_community, :on => :collection
    end
    

    将此视为rake routes

                                listing GET    /listings/:listing_id/:address/:mls(.:format)      listings#show
                            new_text_client POST   /listings/:listing_id(.:format)                    clients#textme
    autocomplete_listing_community_listings GET    /listings/autocomplete_listing_community(.:format) listings#autocomplete_listing_community
                                   listings GET    /listings(.:format)                                listings#index
                                            POST   /listings(.:format)                                listings#create
                                new_listing GET    /listings/new(.:format)                            listings#new
                               edit_listing GET    /listings/:id/edit(.:format)                       listings#edit
                                            PATCH  /listings/:id(.:format)                            listings#update
                                            PUT    /listings/:id(.:format)                            listings#update
                                            DELETE /listings/:id(.:format)                            listings#destroy
    

    【讨论】:

    • 您不能使用相同的路由名称两次,您需要考虑一次,路由始终是唯一的,请参阅文档guides.rubyonrails.org/…
    • 这看起来很不错,但我在运行 rake routes 时仍然遇到命名错误。
    • 在我的手上,它正在工作,你已经看到我的回答了,对吧?我觉得哪里拼错了,你可以仔细检查一下
    • 我看到并尝试过,但是我尝试的时候它没有读取我的限制。
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多