哪来的合适的地方来处理写入current_user.id到对象 [英] where's the appropriate place to handle writing the current_user.id to an object

查看:371
本文介绍了哪来的合适的地方来处理写入current_user.id到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用设计,但已经实现了一个简单的身份验证方案(这里主要介绍 HTTP: //railscasts.com/episodes/250-authentication-from-scratch )的相关部分在这里:

I am not using Devise but have implemented a simple authentication scheme (basically outlined here http://railscasts.com/episodes/250-authentication-from-scratch) with the relevant part being here:

application_controller.rb

application_controller.rb

helper_method :current_user

private
def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

我有一个用户必须被授权来添加资产的列表。我使用的回形针。用户可以HAS_MANY和belongs_to的用户的资产(虽然这是基本无关在那里,因为我的资产模型分配是多态的不同assetable_types)。

I have a list of assets that a user must be authorized to add. I am using paperclip. A user can has_many and a asset belongs_to a user (although this is essentially irrelevant to where it is assigned since my asset model is polymorphic for different assetable_types).

我应该在哪里分配CURRENT_USER ID为资产?我想在模型中;也许我应该做的使用会话default_values​​ [:user_id的。但是,这似乎是有点丑

Where should I assign the current_user id to an asset? I would think in the model; maybe I should do a default_values using the session[:user_id] but that seems to be kinda ugly.

另外,这些都是nested_attributes并且这些嵌套的机型,目前不知道用户什么。信息的CURRENT_USER所以,真正的源不是当前联想的一部分。

Also, these are nested_attributes and the models that these are nested to, currently don't know anything about the user. So really the source of information for the current_user isn't part of the current association.

THX

编辑1 我应该创建一个用户的基于会话实例[:USER_ID]值,或只是将其推入

edit 1 should I create an instance of a User based upon the session[:user_id] value or just push it in?

推荐答案

如果我正确地理解你的问题,为什么不将用户分配到哪个控制器首先发现了该资产所属的用户的资产?它的控制器负责翻译Web请求(包括会话/当前用户)成适用于该模型的东西。

If I understand your question correctly, why not assign the user to the asset in whichever controller first finds out that the asset belongs to the user? It's the controller's responsibility to translate web requests (including the session / current user) into something applicable to the model.

这篇关于哪来的合适的地方来处理写入current_user.id到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆