【发布时间】:2012-11-13 20:05:32
【问题描述】:
我正在尝试将 current_user 值放入模型中。我知道这可能不合适,因为模型应该远离这种类型的交互,但我遇到了问题。我需要在模型的方法中包含 current_user,但不知道该怎么做。
我需要在我的阶段控制器的更新中实现这一点,并将 current_user 传递给阶段模型并让 current_user 值可用。任何帮助表示赞赏。
def update
if @stage.update_attributes(params[:stage])
redirect_to [@project, @stage], :notice => 'Stage was successfully updated.'
else
render :action => "edit"
end
end
【问题讨论】:
-
为什么不创建一个自定义方法然后传递呢?所以你可以不做
@stage.update_attributes(params[:stage]),而是做@stage.update_from_controller(params[:stage], current_user.id),然后让那个方法做任何你想做的事情。 -
这类似于我已经回答的问题:stackoverflow.com/questions/9400321/…
-
你能给我一个更清楚的例子来说明你将如何创建那个方法吗?谢谢!
标签: ruby-on-rails model