[英]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.