2015-09-04 70 views
-2

我试图在我的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)

+0

尝试在'form_tag'中将'med_search'替换为'med_search_path',您需要在使用路由时追加'_path'。您还需要将'med_id'传递给操作或将成员之间的路由更改为集合,具体取决于您的要求 – Athar

+0

我认为这可能是有效的,因为现在我得到一个不同的错误:'没有路由匹配{:action =>“search “,:controller =>”meds“}缺少必需的键:[:med_id]'。我可以看到这与我的路线相符,但我不认为我希望这样设置。如何在我搜索之前更改路线,使其不需要med_id? – winston

+0

要粘贴为答案。不能在这里写太多的代码。 – Athar

回答

1

1)你需要改变你的form_tag这样

<%= form_tag(search_meds_path, :method => "get", id: "search-form") do %> 

2)你需要你的路线从member改变收集这样

路线
resources :meds do 
    collection do 
    get 'search' => 'meds#search' 
    end 
end 

3)不知道为什么你需要添加resources :meds, :path => '' at底部再次。 Incase你不需要它,最好删除。

+0

在routes.rb上获取'unexpected keyword_end,期待输入结束(SyntaxError)'。使用当前路线格式更新的问题 – winston

+0

在回答中的代码中完成了'end'。是否有一些额外的路线文件 – Athar

+0

是我需要添加devise_for用户,根目录索引,并且'resources:meds,:path =>'''for friendly_id slugs – winston

0

它看起来像你试图编码搜索,但因为你把它放在资源块中Rails假设你正在谈论一个特定的med

从资源块中删除路线并将其更改为get 'meds/search' => 'meds#search'。这将允许您将它作为常规端点使用,而无需Rails抱怨您需要一个ID。

相关问题