Rails错误resource_name - 设计帮助路由和呈现

时间:2011-05-10 19:15:23

标签: ruby-on-rails ruby ruby-on-rails-3 devise

我正在尝试渲染Devise gem的登录视图但是我收到错误,下面是我目前的代码:

这是我的views/users/shared/_links.html.erb

<%- if controller_name != 'sessions' %>
  <%= link_to "Sign in", new_session_path(resource_name) %><br />
<% end -%>

<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
  <%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>

<%- if devise_mapping.recoverable? && controller_name != 'passwords' %>
  <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end -%>

<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
  <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
  <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
<% end -%>

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
    <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
  <% end -%>
<% end -%>

我的config/routes.rb

Densidste::Application.routes.draw do
  match 'user/edit' => 'users#edit', :as => :edit_current_user

  match 'signup' => 'devise/users#new', :as => :signup

  match 'logout' => 'devise/sessions#destroy', :as => :logout

  devise_for :users do
    get "login", :to => "devise/sessions#new"
  end

  resources :sessions

  resources :users

  devise_for :users

  resources :aktivs

  resources :taggingposts

  resources :tags

  resources :kommentares

  resources :posts

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  root :to => "public#index"
end

在我的应用程序布局中:

<%= render("users/shared/links") %>

我在_links.html.erb partial:

中收到以下错误
NameError in Public#index

Showing C:/Rails/Densidste/app/views/users/shared/_links.erb where line #2 raised:

undefined local variable or method `resource_name' for #<#<Class:0x5db76c0>:0x5db6538>

Extracted source (around line #2):

1: <%- if controller_name != 'sessions' %>
2:   <%= link_to "Sign in", new_session_path(resource_name) %><br />
3: <% end -%>
4: 
5: <%- if devise_mapping.registerable? && controller_name != 'registrations' %>

Trace of template inclusion: app/views/public/index.html.erb

Rails.root: C:/Rails/Densidste

最后,在我的应用程序控制器中,我有以下内容:

before_filter :resource_name
  def resource_name
    if user_signed_in?
      current_user.name
    else
      :user
    end
  end
end

非常感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:15)

resource_name在Devise生成的控制器中定义。我不认为您可以在应用程序布局中包含这些共享链接,我认为它们旨在用于设计表单(注册,会话,密码,确认等),这些表单由设计控制器呈现。

如果您希望每个页面都有很少的登录/退出片段,您可能需要考虑自己编写这些链接。例如,如果您正在使用的对象是user,那么您可以这样写:

<%= link_to "Sign in", new_session_path(:user) %><br />

resource_name只是您正在使用的资源的Devise抽象。我希望如果你正在建立这个链接,你知道你想要登录的认证对象,所以你可以明确地指定它。您也可以运行rake routes | grep sessions并查看路径的名称并直接使用它。例如:

<%= link_to "Sign in", new_user_session_path %><br />

答案 1 :(得分:0)

问题是,在routes.rb文件中,您正在定义名为sessions的资源,设计也有一个名称相同的资源。

解决方案很简单,只需从路径文件中删除resources :sessions,一切都应该有效。

发生这种情况的原因是rails为所有资源创建了路由助手。因此它创建了一个名为new_session_path(format)的路由助手。但是设计也会创建一个名为new_session_path(resource_name)的辅助方法。设计版本返回路径new_<resouce_name>_session_path。换句话说,路由文件生成的助手会覆盖设计中的助手。

请注意,如果您执行以下操作,也会发生这种情况:

devise_scope :user do 
  get 'login', :to => 'devise/sessions#new', :as => :new_session
  get 'logout', :to => 'devise/sessions#destroy', :as => :destroy_session
end

在这种情况下,解决方案是分别用new_sessiondestroy_session替换loginlogout

答案 2 :(得分:0)

您可以使用以下替换您的app / views / devise / confirmation / new.html文件来解决此错误

https://github.com/heartcombo/devise/pull/3684/files

在上面的链接中包含文件app / views / devise / confirmation / new.html 您可以从上方链接中找到该文件并替换为文件,因为在最新版本中进行了一些更改,因此请访问上方链接并从上方链接中找到app / views / devise / confirmation / new.html文件并将其替换为您的项目文件app / views / devise / confirmation / new.html文件