我试图在我的Rails应用程序中创建一个简单的搜索表单。我得到一个错误与形式的URL路径:未定义的局部变量或方法与form_tag
<%= form_tag(med_search, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<%= button_to "Search", class: "btn btn-default" %>
<% end %>
上面的第一行会导致错误undefined local variable or method
med_search”为#<#:0x007fab2b5afa90>`
问题是最有可能与我的路线设置。我创建了一个新的控制器动作称为搜索,所以我编辑我routes.db看起来像这样:
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
devise_for :users
#get 'meds/index'
root to: "meds#index"
resources :meds, :path => ''
end
当我做耙路线,我看到了路径检索配有,所以我知道该网址是有效的:
Prefix Verb URI Pattern Controller#Action
med_search GET /meds/:med_id/search(.:format) meds#search
meds GET /meds(.:format) meds#index
POST /meds(.:format) meds#create
new_med GET /meds/new(.:format) meds#new
edit_med GET /meds/:id/edit(.:format) meds#edit
med GET /meds/:id(.:format) meds#show
PATCH /meds/:id(.:format) meds#update
PUT /meds/:id(.:format) meds#update
DELETE /meds/:id(.:format) meds#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / meds#index
GET / meds#index
POST / meds#create
GET /new(.:format) meds#new
GET /:id/edit(.:format) meds#edit
GET /:id(.:format) meds#show
PATCH /:id(.:format) meds#update
PUT /:id(.:format) meds#update
DELETE /:id(.:format) meds#destroy
我应该在路线中改变什么来解决这个问题?
谢谢!
编辑:将网址更改为med_search_path,得到新的错误:No route matches {:action=>"search", :controller=>"meds"} missing required keys: [:med_id]
。看起来像它涉及到/meds/:med_id/search(.:format)
尝试在'form_tag'中将'med_search'替换为'med_search_path',您需要在使用路由时追加'_path'。您还需要将'med_id'传递给操作或将成员之间的路由更改为集合,具体取决于您的要求 – Athar
我认为这可能是有效的,因为现在我得到一个不同的错误:'没有路由匹配{:action =>“search “,:controller =>”meds“}缺少必需的键:[:med_id]'。我可以看到这与我的路线相符,但我不认为我希望这样设置。如何在我搜索之前更改路线,使其不需要med_id? – winston
要粘贴为答案。不能在这里写太多的代码。 – Athar