我正在尝试呈现 Devise gem 的登录 View ,但出现错误,下面是我目前拥有的代码:
这是我的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
最后,在我的应用程序 Controller 中,我有以下内容:
before_filter :resource_name
def resource_name
if user_signed_in?
current_user.name
else
:user
end
end
end
非常感谢任何帮助,谢谢!
最佳答案
resource_name
在 Devise 生成的 Controller 中定义。我不认为您可以在应用程序布局中包含这些共享链接,我认为它们旨在用于由设计 Controller 呈现的设计表单(注册、 session 、密码、确认等)。
如果您希望在每个页面中都有很少的登录/退出片段,您可能需要考虑自己编写这些链接。例如,如果你使用 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 />
关于ruby-on-rails - Rails error resource_name - 设计帮助路由和渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5955224/