简体   繁体   English

在rails“获取'vendors / keyword_search',到:'vendors#keyword_search'”中运行“'vendors#show'”

[英]In rails “get 'vendors/keyword_search', to: 'vendors#keyword_search'” runs “'vendors#show'”

In a rails app, I am trying to setup a route to a keyword search page. 在Rails应用程序中,我正在尝试设置通往关键字搜索页面的路线。 I am trying to pass the following test: 我正在尝试通过以下测试:

scenario 'can perform a keyword search' do
  click_link 'Search'
  click_link 'Keyword search'
  expect(current_path).to eq '/vendors/keyword_search'
  fill_in 'search', with: vendor_one.email
  click_button 'Search suppliers'
  expect(page).to have_content vendor_one.email
  expect(page).not_to have_content vendor_two.email
end

So far I have: 到目前为止,我有:

in /views/vendors/index.html.erb /views/vendors/index.html.erb中

<div class='col-xs-3'>
  <p><%= link_to 'Keyword search', vendors_keyword_search_path %></p>
</div>

in /routes.rb /routes.rb中

root to: 'homepage#index'

resources :buyers, :vendors

get 'vendors/keyword_search', to: 'vendors#keyword_search'

in /controllers/vendors_controller.rb /controllers/vendors_controller.rb中

class VendorsController < ApplicationController
  def index
    @vendors = Vendor.all
  end

  def show
    @vendor = Vendor.find(params[:id])
  end

  def keyword_search
  end
end

I then have a /views/vendors/keyword_search.html.erb with the following: 然后,我有一个/views/vendors/keyword_search.html.erb,其中包含以下内容:

<div id='main container' class='container-fluid'>
  <div class='row'>
    <div class='col-xs-3'>
    </div>
    <div class='col-xs-6'>
      <h1>suppliers#keyword_search</h1>
    </div>
    <div class='col-xs-3'>
    </div>
  </div>
</div>

My issue seems to be coming after a click on the 'Keyword search' link. 单击“关键字搜索”链接后,似乎出现了我的问题。 Rather than running vendors#keyword_search and loading the ' /views/vendors/keyword_search.html.erb ' template, my path helper vendors_keyword_search_path attempts to run vendors#show resulting in the following error message (note the first line of stack trace included): 我的路径助手vendors_keyword_search_path尝试运行vendors_keyword_search_path vendors#show而不是运行vendor vendors#keyword_search并加载' /views/vendors/keyword_search.html.erb '模板,从而导致出现以下错误消息(请注意所包含的堆栈跟踪的第一行):

1) Buyers searching vendors can perform a keyword search
   Failure/Error: @vendor = Vendor.find(params[:id])

   ActiveRecord::RecordNotFound:
     Couldn't find Vendor with 'id'=keyword_search
   # ./app/controllers/vendors_controller.rb:7:in `show'

Help on this would be greatly appreciated, but most of all I really want to understand why it's running show and not keyword_search . 对此的帮助将不胜感激,但最重要的是,我真的很想了解为什么它正在运行show而不是keyword_search

Thanks 谢谢

Rails processes the routes sequentially. Rails按顺序处理路线。 So it matches vendors/keyword_search to vendors#show as that is the first "match" in the routes. 因此,它将供应商/关键字搜索与供应商#显示匹配,因为这是路由中的第一个“匹配”。 It then treats keyword_search as the :id parameter for the show route. 然后,它将keyword_search视为显示路线的:id参数。

So you should place your route for keyword_search above the resources. 因此,应将您的keyword_search路线放置在资源上方。

Try after changing the below line in your route file 更改路由文件中的以下行后尝试

get 'vendors/keyword_search', to: 'vendors#keyword_search'

to

match "vendors/keyword_search" => "vendors#keyword_search", :as => :vendors_keyword_search_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取所有供应商的列表? - Getting list of all vendors? Rails服务器端工作以在QuickBooks Online中创建供应商 - Rails server side job to create vendors in QuickBooks Online ActionController :: RoutingError:没有路由与{:controller =&gt;“ vendors”,:action =&gt;“ vendors”}匹配? - ActionController::RoutingError: No route matches {:controller=>“vendors”, :action=>“vendors”}? 如何基于搜索关键字栏从2个表中获取数据 - How to get data from 2 tables based on search keyword rails Ubuntu上的Phusion Passenger在vendor目录中未看到插件 - Phusion Passenger on Ubuntu not seeing plugin in vendors directory Spree Commerce供应商列表页[NoMethodError:#的未定义方法&#39;each&#39; <Class:…> ] - Spree Commerce Vendors Listing Page [NoMethodError : undefined method `each' for #<Class:…>] 多关键字搜索 - Multi keyword search 如何使上限部署链接到我的供应商文件夹? - How to make cap deploy link my vendors folder? Ruby on rails 如何按关键字搜索我的表? - Ruby on rails how can I search my table by keyword? Ruby on Rails:带有关键字和日期范围的搜索表单不起作用 - Ruby on Rails : Search form with keyword and date range does not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM