新手问题 使用 gem 插件'notifications'后出现的一个小问题

msl12 · 2016年04月16日 · 最后由 msl12 回复于 2016年04月16日 · 2632 次阅读
Showing /sample/cmplan/app/views/layouts/application.html.erb where line #20 raised:

undefined local variable or method `root_path' for #<#<Class:0x000000064a62e8>:0x007f16117ed078>
Extracted source (around line #20):              
<%= user_name_tag(current_user) %>(管理员)<% end %>
<%= link_to '首页', root_path %>
<%= link_to '编辑资料', edit_user_registration_path %>
<% if admin? %>
<%= link_to '撰写公告', edit_announcement_path %>
<% end %>

这个是我在浏览器输入 url 为‘/notifications’后出现的一个错误页面,它读取不到路由里的 root_path 和 edit_user_registration_path 等,而其它页面并没有这个问题

我猜应该是使用 gem 插件'notifications'后,我没有正确设置什么东西吧?

我是看 Ruby-China 用到了这个管理消息推送的插件后也试着使用一下。

请问我该怎么解决这个问题呢?

Started GET "/notifications" for 127.0.0.1 at 2016-04-16 13:57:08 +0800
Processing by Notifications::NotificationsController#index as HTML
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 9]]
  Notification Load (0.4ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1  ORDER BY id desc LIMIT 20 OFFSET 0  [["user_id", 9]]
  Rendered notifications/notifications/index.html.erb within layouts/application (4.9ms)
Completed 500 Internal Server Error in 128ms (ActiveRecord: 0.6ms)

ActionView::Template::Error (undefined local variable or method `root_path' for #<#<Class:0x007f3740d9c348>:0x007f37410efd88>):
    17: <% else %>
    18: <%= user_name_tag(current_user) %>(管理员)<% end %>
    19: <%= link_to '首页', root_path %>
    20: <%= link_to '编辑资料', edit_user_registration_path %>
    21: <% if admin? %>
    22: <%= link_to '撰写公告', edit_announcement_path %>
    23: <% end %>
  app/views/layouts/application.html.erb:20:in `_app_views_layouts_application_html_erb__959845113685053431_69937645716180'

不明白为什么,routes 文件里面是已经有root to: 'welcome#index'

有汉字,是编码问题?

#3 楼 @ken 不会吧?只是‘/notifications’这个页面才会出现这个情况哦

试试 main_app.root_path

#5 楼 @imconfused 你这么一说,噢,好像知道了什么了

msl12 关闭了讨论。 10月17日 08:52
需要 登录 后方可回复, 如果你还没有账号请 注册新账号