【问题标题】:Rails error resource_name - devise help routing and renderingRails 错误资源名称 - 设计帮助路由和渲染
【发布时间】:2011-05-10 19:15:22
【问题描述】:

我正在尝试为 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 部分中收到以下错误:

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

任何帮助将不胜感激,谢谢!

【问题讨论】:

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


    【解决方案1】:

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

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

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

    resource_name 只是您正在使用的资源的设计抽象。我希望,如果您正在创建此链接,您知道 哪些 您想要登录的经过身份验证的对象,因此您可以明确指定它。您也可以运行 rake routes | grep sessions 并查看路径的名称并直接使用它。例如:

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

    【讨论】:

    • 我已经更新了我的问题如何创建 before_filter :resource_name?
    • 我相信resource_name一般是类名的下划线版本。你可以试试current_user.class.name.underscore。这应该为User 类型的对象提供user 或为FancyUser 类的对象提供fancy_user
    • devise/controllers/url_helpers.rb中有一些关于这些“资源特定”辅助方法的有用的cmets
    • 感谢这项工作。我用 new_user_registration_path 替换了 new_registration_path(resource_name) 它现在可以工作了。
    【解决方案2】:

    问题是在您的routes.rb 文件中定义了一个名为sessions 的资源,devise 也有一个同名的资源。

    解决方案很简单,只需从路由文件中删除 resources :sessions 即可,一切正常。

    发生这种情况的原因是因为 rails 会为所有资源创建路由助手。因此它创建了一个名为 new_session_path(format) 的路由助手。但设计也创建了一个名为new_session_path(resource_name) 的辅助方法。设计版本返回路径new_&lt;resouce_name&gt;_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

    【讨论】:

      【解决方案3】:

      您可以通过将 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 文件

      【讨论】:

      • 应该发布代码而不是链接。答案不应依赖于外部来源。
      猜你喜欢
      • 2010-12-11
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多