Rails 的 link_to 返回带有点而不是斜线的 url

Pau*_*rit 3 ruby haml ruby-on-rails

Rails 3.0.9 withhaml gem & Ruby 1.9.2-head with rvm

我有订单资源。

route.rb 文件的片段

resources :orders
Run Code Online (Sandbox Code Playgroud)

使用 Order 模型实例调用 link_to 助手返回 /order.2 而不是 /orders/2。

order_controller.rb 和 index.html.haml 的片段

#index.haml.html
%ul
  - @orders.each do |item|
    %li= link_to item.id, item #=> <a href="/order.2">2</a> instead of  <a href="/orders/2">2</a>

#orders_controller.rb 
def index 
  @orders = Order.all
 end
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

我还有其他资源,但它们工作正常。

更新:

我的 routes.rb 文件列表

  YetApp::Application.routes.draw do

  resources :categories, :products, :images, :orders, :small_images

  match "/order", :to => "orders#new", :as=> 'order'

  match "/success/:id", :to => "orders#success", :as=> 'order'

  #namespace :signed do
  #  resources :products, :images, :categories
  #end

  root :to => 'pages#home'

  match '/signed', :to => 'pages#signed', :as => 'signed'


  match '/cooperation', :to => 'pages#cooperation', :as => 'cooperation'
  match '/payment', :to => 'pages#payment', :as => 'payment'
  match '/offer', :to => 'pages#offer', :as => 'offer'
  match '/order', :to => 'pages#order', :as => 'order'

end
Run Code Online (Sandbox Code Playgroud)

nat*_*vda 5

我认为你应该删除

match '/order', :to => ...
Run Code Online (Sandbox Code Playgroud)

从你的路线文件。

  • 您实际上还有另外两条与 /order 路径匹配的路由。当 Rails 尝试解析命名路由时,这些会导致冲突。删除那些,你应该做生意 (2认同)