Gem 在 Cells 里面调用 Devise 的 current_user 方法

huacnlee · August 06, 2012 · Last by evan replied at August 07, 2012 · 3451 hits

之前一直很苦恼这个事情,在 Cells 里面无法直接调用 Devise 提供的 current_user 方法。用了土鳖的方法,通过 render_cell 把 current_user 当成一个参数传进去...

今天查了,原来可以直接引入 Devise 的 current_user 到 Cells 里面...

https://github.com/apotonick/cells/issues/23

具体实现就是:

做个 base_cell.rb

class BaseCell < Cell::Rails
  include Devise::Controllers::Helpers
  helper_method :current_user
end

然后让所有的 Cell 继承 BaseCell,比如 users_cell.rb

class UsersCell < BaseCell
  def sidebar_user_followable_info(user)
    @user = user || current_user
    render
  end
end

我还是觉得 cell 没啥必要,helper + partial template 已经可以完成它的功能。

#1 楼 @Rei cell 是给有“不能在 view 里面写逻辑”的强迫症孩子用的

#2 楼 @ywencn 放在 helper 目录下面的代码,和放在 cell 目录下的代码,会有多少区别呢?而且我觉得 cell 的调用层次更深。

@Rei 通过 Cells 整理出来以后,结构更加清晰,在 Helper 里面写查询怪怪的,也没法 render html 模版。

Cells 带来了:

  • render 一个 html 模版,并且这个模版是不会混在普通 action 的模版里面,结构更佳清晰
  • caching 处理功能
  • 在里面写复杂的逻辑会比 Helper 里面好写一些

没试过 Helper 直接 render 一个 html 文件,不知道是否可以?

#4 楼 @huacnlee 我一般会在 helper

render :patial => 'shared/name'

help 里的 render 跟 view 里面应该一样用。

据说用 cells 有利于提高缓存性能?

You need to Sign in before reply, if you don't have an account, please Sign up first.