新手问题 Devise 中 resource,和 resource_name 是啥意思

evil850209 · 2013年01月14日 · 最后由 evil850209 回复于 2013年01月15日 · 3991 次阅读

看 github 中有一个 Devise,Canan,SimpleForm 的例子,其中有一段代码不太明白。

<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %> <%= f.input :email, :autofocus => true %> <%= f.input :password %> <%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %> <%= f.button :submit, "Sign in", :class => 'btn-primary' %> <% end %> <%= render "devise/shared/links" %>

想知道 resource,和 resource_name 是什么意思,session_path 应该是 devise 生成的吧?

原项目的地址 http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html

一般来说 resource 就是 User 的实例,resource_name 就是 "user"

不过也可以是 Admin 的实例和 "admin" 等其他的模型,这么做是为了让 model 名称可以配置

devise 可以配置多个 resource,每个 resource 就是一整套用户系统 比如 User 是一个 resource,Admin 也是一个 resource,你也可以配置任意名称的

在官方文档里没有看到具体 resource 的设置,有没有更详细的文档,说一下 Devise 中的 resource 到底是怎么设置的? @luikore @suupic

#3 楼 @evil850209 以源码为准啦... 我记得 devise 源代码里设置 resource 的部分挺绕来绕去的...

@luikore 看源码对我一个新手有点困难,:(

#5 楼 @evil850209

resource= https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb#L92

resource https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb#L16

sign_in https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L114

也就是说 sign_in 的时候会给 "@#{resource_name}" 赋值,当然还有几个地方会对 "@#{resource_name}" 进行修改的,具体的可以搜索下 resource =resource ||=resource= 这些

@vkill 强人呀!学习中

需要 登录 后方可回复, 如果你还没有账号请 注册新账号