【问题标题】:How to fix an error with routing in rails app?如何修复rails应用程序中的路由错误?
【发布时间】:2013-05-20 14:26:21
【问题描述】:

我的 routes.rb 文件中有以下内容:

match "/service/:product"           => "products#show_name"
match "/products/custom/"           => "products#custom"
match "/products/customform"        => "products#customform"
match "/categories"                 => "products#categories"
match "/search"                     => "products#search"
match "/about"                      => "products#about"

get 'category/:tag', to: 'products#category', as: :tag

resources :products do
    resources :reviews
end

我正在尝试访问网址“/service/birchbox”。当我这样做时,我收到一个错误提示

No route matches {:action=>"edit", :controller=>"products"}

关于如何解决这个问题的任何建议?我试图让这个 url 转到列出的第一条路线(到控制器 products#show_name 但出于某种原因要编辑)。

这是我在控制台中看到的更多信息。似乎它会显示名称,但我不断收到错误消息。

Processing by ProductsController#show_name as HTML
  Parameters: {"product"=>"birchbox"}
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."slug" = 'birchbox' LIMIT 1
   (0.1ms)  SELECT COUNT(*) FROM "reviews" WHERE "reviews"."product_id" = 14
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 14 AND "taggings"."taggable_type" = 'Product' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
  Rendered products/show_name.html.erb within layouts/application (97.3ms)
Completed 500 Internal Server Error in 164ms

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"products"}):
  app/views/products/show_name.html.erb:42:in `_app_views_products_show_name_html_erb___2110533700820522490_70197663436420'

【问题讨论】:

  • 更改后是否重启了服务器?
  • @SergioTulentsev 是的,我做到了。我还用我收到的更多错误更新了问题。

标签: ruby-on-rails routing


【解决方案1】:

似乎在第 42 行的 views/products/show_name.html.erb 中有这样的内容:

<%= edit_product_path %>

而且这条路线不存在。 edit 路线需要产品 ID。试试这个:

<%= edit_product_path(@product) %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多