为什么不能耙路线更有帮助

akk*_*dio 0 routes ruby-on-rails link-to

当您在shell中键入rake路由时,它会显示一个很好的路由列表:

      new_edition GET    /editions/new(.:format)           editions#new
     edit_edition GET    /editions/:id/edit(.:format)      editions#edit
         edition GET    /editions/:id(.:format)           editions#show
                 PUT    /editions/:id(.:format)           editions#update
              DELETE /editions/:id(.:format)           editions#destroy
Run Code Online (Sandbox Code Playgroud)

这非常有用,但为什么不显示需要在应用程序中使用的实际代码

 edition GET    /editions/:id(.:format)  editions#show  edition_path()
Run Code Online (Sandbox Code Playgroud)

我猜这是因为它可能还有更多,但是一般的问题是当我查看示例时给出的路径我查找了一个如何明确编码以了解路由意味着什么的示例...

jdo*_*doe 5

xxx_path直接使用并不是您唯一的选择.

Rails为您提供了通过polymorpic_path/_url方法构建URL的资源丰富的方法.许多其他助手使用这些方法,例如:

link_to 'Edit', [:edit, @user]     # instead of edit_user_path(@user)
redirect_to Product                # instead of products_path
form_for [@order, @product] do |f| # instead of order_product_path(@order, @product)
visit url_for [:preview, @invoice] # instead of preview_invoice_path(@invoice)
Run Code Online (Sandbox Code Playgroud)

因此,通过查看preview_invoice前缀,您知道该怎么做,但确切的方法取决于您.