Rails:路由和路径助手 [英] Rails: routing and path helpers

查看:55
本文介绍了Rails:路由和路径助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Rails:没有复数的路由会产生奇怪的帮手

原来我在 config/routes.rb 中有 :resources qtl_table 两次!我收到此错误:

Turns out I had :resources qtl_table in config/routes.rb twice! I get this error:

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

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

in 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:

and in config/routes.rb:

Qtl::Application.routes.draw do
    resources :qtl_table do
            collection do
                    get 'search'
            end
    end
  ...
end

是的,我确实关闭了复数:

yes I do have plurals turned off:

ActiveRecord::Base.pluralize_table_names = false

耙路线的输出:

              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"}

推荐答案

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

You may have plurals turned off, but this only affects the table names in the database, not how Rails handles routes.

因为search 路由属于一个集合,而不是成员,所以它作用于多个模型对象.因此,路径应该是search_qtl_tables_path,注意复数表.

Because the search route belongs to a collection, not a member, it acts on multiple model objects. Therefore, the route should be search_qtl_tables_path, note plural tables.

qtl_table 是一个模型,你想搜索它们的集合,所以路由读起来像搜索多个记录"是有道理的.

qtl_table is a model, and you want to search a collection of them, so it make senses that the route reads like "search multiple records".

edit:我主要担心的是您的 rake 路由 不应该两次显示那些 qtl_table 路由.您是否在 routes.rb 中的某处重复自己?

edit: My main concern is that your rake routes shouldn't be showing those qtl_table routes twice. Are you repeating yourself somewhere in routes.rb?

好的,你已经删除了额外的路由.现在,这应该有效...

OK, so you've deleted the extra routes. Now, this should work...

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

这篇关于Rails:路由和路径助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆