我的所有POST链接都由“创建”处理

时间:2012-06-29 18:08:57

标签: ruby-on-rails ruby-on-rails-3

我试图从索引操作中对我的数据库进行简单搜索,而不是列出所有数据

这是我的用户/索引

<%= form_tag do %>
<fieldset>
    <div class="row">
        <div class="span5 offset3">
            <h2>Enter the CPF number of the user to be managed: </h2></br></br></br>
            <div>
                <%= label_tag :cpf_no, 'CPF Number:' ,class:"left_align" %>
                <%= number_field_tag :cpf_no, params[:cpf_no] %>
            </div>
            <div>
                <%= button_to  "     Find     ", users_find_path , class: "btn btn-large btn-primary" %></br></br>
            </div>
        </div>
    </div>
  </fieldset>
<% end %>

这是我控制器中的find方法:

 class UsersController < ApplicationController   
  ...
def find 
if request.post?
@user = user.find_by_cpf_no(params[:cpf_no])
redirect_to edit_user_path(@user.id)
end     
end 
...
end

现在这是我的路线:

              root        /                                home#index
             login GET    /global/login(.:format)          sessions#new
                   POST   /global/login(.:format)          sessions#create
            logout DELETE /global/logout(.:format)         sessions#destroy
        users_find POST   /global/users/find(.:format)     users#find
             users GET    /global/users(.:format)          users#index
                   POST   /global/users(.:format)          users#create
          new_user GET    /global/users/new(.:format)      users#new
         edit_user GET    /global/users/:id/edit(.:format) users#edit
              user GET    /global/users/:id(.:format)      users#show
                   PUT    /global/users/:id(.:format)      users#update
                   DELETE /global/users/:id(.:format)      users#destroy
            fields GET    /global/data(.:format)           fields#index
                   POST   /global/data(.:format)           fields#create
         new_field GET    /global/data/new(.:format)       fields#new
        edit_field GET    /global/data/:id/edit(.:format)  fields#edit
             field GET    /global/data/:id(.:format)       fields#show
                   PUT    /global/data/:id(.:format)       fields#update
                   DELETE /global/data/:id(.:format)       fields#destroy

问题是它没有转到:id / edit路径...而是调用create方法,它会产生一些错误,因为验证没有通过。如何让它访问查找方法?

1 个答案:

答案 0 :(得分:1)

您应该将路径放在form_tag中。

form_tag user_find_path, :method => :post
    # other code
    = submit_tag "Find"