【问题标题】:link_to has no controller and no actionlink_to 没有控制器也没有动作
【发布时间】:2012-09-27 13:53:15
【问题描述】:

我对以下链接生成器有一个看法:

- @tags.each do |tag|
  = link_to tag.name, :controller => "images", :action => "#{tag.name}"

@tagsTag.all。它显示具有正确名称的链接,鼠标悬停显示例如

http://localhost:3000/Images/tagname

但是如果我点击一个错误是:

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

当我指定:controller => "images"时,它怎么能说:controller是空的?

rake 路线(相关部分):

images GET           /images(.:format)                     images#index
POST                 /images(.:format)                     images#create
new_image GET        /images/new(.:format)                 images#new
edit_image GET       /images/:id/edit(.:format)            images#edit
image GET            /images/:id(.:format)                 images#show
PUT                  /images/:id(.:format)                 images#update
DELETE               /images/:id(.:format)                 images#destroy
tags GET             /Images(.:format)                     tags#index
POST                 /Images(.:format)                     tags#create
new_tag GET          /Images/new(.:format)                 tags#new
edit_tag GET         /Images/:id/edit(.:format)            tags#edit
tag GET              /Images/:id(.:format)                 tags#show
PUT                  /Images/:id(.:format)                 tags#update
DELETE               /Images/:id(.:format)                 tags#destroy

一个标签,例如是“saturn”,所以我也在routes.rb中添加了这条路线:

match '/Images/saturn'                => 'images#saturn'

导致:

Images_saturn        /Images/saturn(.:format)              images#saturn

我的图像控制器中还有一个方法“saturn”。

【问题讨论】:

  • 你能把rake routes的结果贴在这里吗?
  • 更多堆栈跟踪?很可能是您链接的视图 to 包含错误的 url 帮助方法,而不是带有这些标记链接的视图。毕竟,如果您在此视图中遇到路由错误,您会在尝试加载它时收到错误,而不是在您尝试离开它时。
  • 我已经添加了 rake 路由结果。

标签: ruby-on-rails ruby-on-rails-3 routing link-to


【解决方案1】:

在我看来,您遇到了大小写问题。您的路线中有 /images 和 /Images ...您可以尝试修复它吗?

【讨论】:

    【解决方案2】:

    试试

    = link_to tag.name, {:controller => "images", :action => "#{tag.name)}"}

    【讨论】:

      【解决方案3】:

      之后你有一个额外的“)”
      "#{tag.name)}"
      

      【讨论】:

      • 对不起,我的错,在代码中它是正确的。我现在已经在上面的问题中更正了。
      猜你喜欢
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多