开发者

form_tag not working as expected

开发者 https://www.devze.com 2023-03-27 20:27 出处:网络
I\'ve added a method to my controller and routed it correctly but when I try to call it from a form_tag it give me a router error. What\'s going on?

I've added a method to my controller and routed it correctly but when I try to call it from a form_tag it give me a router error. What's going on?

<% form_tag search_item_path, :method => 'get' do %>
  <%= text_field_tag :name , '' %>
  <%= submit_tag "Submit" %>
<% end %>

routes:

resources :items do
   collection do
      get :search, :as => :search
   end
end

rake routes also ok:

开发者_Python百科
search_item GET    /items/:id/search(.:format)                        {:action=>"search", :controller=>"items"}
      items GET    /items(.:format)                                   {:action=>"index", :controller=>"items"}
            POST   /items(.:format)                                   {:action=>"create", :controller=>"items"}
   new_item GET    /items/new(.:format)                               {:action=>"new", :controller=>"items"}
  edit_item GET    /items/:id/edit(.:format)                          {:action=>"edit", :controller=>"items"}
       item GET    /items/:id(.:format)                               {:action=>"show", :controller=>"items"}
            PUT    /items/:id(.:format)                               {:action=>"update", :controller=>"items"}
            DELETE /items/:id(.:format)                               {:action=>"destroy", :controller=>"items

However, if I write something like this works:

<% form_tag url_for(:controller => "items" , :action => "search"), :method => "get" do %>

What am I missing here?


I believe it should be pluralized search_items_path

And routes could be little cleaner

resources :items do
  collection do
    get :search
  end
end

or

resources :items do
  get :search, :on => :collection
end


Your route is looking for an id, and must be called with search_item_path(@item)?

Something is not right there. With the routes.rb you gave, it should look like:

search_items GET /items/search(.:format)

Are we seeing everything here? Your sample defines a collection route but the output of your routes.rb shows it as a member route.

0

精彩评论

暂无评论...
验证码 换一张
取 消