Rails:路由和路径助手

时间:2012-01-04 15:12:59

标签: ruby-on-rails routes

  

可能重复:
  Rails: Routing without plurals gives strange helpers

原来我有:资源qtl_table在config / routes.rb中两次!我收到这个错误:

undefined local variable or method `search_qtl_table_index' for #<#<Class:0x805aff3e0>:0x805af47b0>

在app / views / qtl_table / index.html.erb中:

<h2>Search for QTL</h2>
<%= form_tag search_qtl_table_index, :method => 'get' do %>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
<% end %>

和config / routes.rb:

Qtl::Application.routes.draw do
    resources :qtl_table do
            collection do
                    get 'search'
            end
    end
  ...
end
是的,我确实关闭了复数:

ActiveRecord::Base.pluralize_table_names = false

rake路线的输出:

              search_qtl_table_index GET    /qtl_table/search(.:format)                            {:action=>"search", :controller=>"qtl_table"}
                     qtl_table_index GET    /qtl_table(.:format)                                   {:action=>"index", :controller=>"qtl_table"}
                                     POST   /qtl_table(.:format)                                   {:action=>"create", :controller=>"qtl_table"}
                       new_qtl_table GET    /qtl_table/new(.:format)                               {:action=>"new", :controller=>"qtl_table"}
                      edit_qtl_table GET    /qtl_table/:id/edit(.:format)                          {:action=>"edit", :controller=>"qtl_table"}
                           qtl_table GET    /qtl_table/:id(.:format)                               {:action=>"show", :controller=>"qtl_table"}
                                     PUT    /qtl_table/:id(.:format)                               {:action=>"update", :controller=>"qtl_table"}
                                     DELETE /qtl_table/:id(.:format)                               {:action=>"destroy", :controller=>"qtl_table"}

2 个答案:

答案 0 :(得分:1)

您可能关闭了复数,但这只会影响数据库中的表名,而不会影响Rails处理路由的方式。

由于search路由属于集合,而不是成员,因此它会作用于多个模型对象。因此,路线应为search_qtl_tables_path,注意多个表格。

qtl_table是一个模型,您想要搜索它们的集合,因此它会使路线读起来像“搜索多个记录”。

编辑:我主要担心的是您的rake routes不应该两次显示这些qtl_table路线。你是在routes.rb的某个地方重复自己吗?

好的,所以你删除了额外的路线。现在,这应该有用......

<%= form_tag search_qtl_table_index_path, :method => 'get' do %>

答案 1 :(得分:0)

尝试:

Qtl::Application.routes.draw do
    resources :qtl_table do
        collection do
                get 'search', :as => 'search_qtl_table'
        end
    end
  ...
end
相关问题