Ruby on rails rails,`flash[:notice]`在我的模型中?
我有一个用户模型,在这个模型中,我有一个方法可以查看用户是否获得了徽章Ruby on rails rails,`flash[:notice]`在我的模型中?,ruby-on-rails,application-design,Ruby On Rails,Application Design,我有一个用户模型,在这个模型中,我有一个方法可以查看用户是否获得了徽章 def check_if_badges_earned(user) if user.recipes.count > 10 award_badge(1) end 如果用户已获得徽章,则运行奖励徽章方法并向用户提供关联的徽章。我可以这样做吗 def check_if_badges_earned(user) if user.recipes.count > 10 flash.now[:notice] =
def check_if_badges_earned(user)
if user.recipes.count > 10
award_badge(1)
end
如果用户已获得徽章,则运行奖励徽章方法并向用户提供关联的徽章。我可以这样做吗
def check_if_badges_earned(user)
if user.recipes.count > 10
flash.now[:notice] = "you got a badge!"
award_badge(1)
end
奖金问题!瘸子,我知道
对于我来说,哪里是保存所有这些条件的最佳位置,我的用户可以获得类似于stackoverflows徽章的徽章。我的意思是在建筑方面,我已经有了徽章和徽章模型
我如何组织他们获得的条件?其中一些是复杂的,比如用户已经登录了100次而没有评论过一次。等等。因此,似乎没有一个简单的地方来放置这种逻辑,因为它几乎涵盖了所有模型。我很抱歉,在模型中无法访问flash哈希,它是在控制器中处理请求时创建的。您仍然可以使用实现您的方法来存储属于用户的badge对象中包含的badge infos flash消息:
class Badge
# columns:
# t.string :name
# seed datas:
# Badge.create(:name => "Recipeador", :description => "Posted 10 recipes")
# Badge.create(:name => "Answering Machine", :description => "Answered 1k questions")
end
class User
#...
has_many :badges
def earn_badges
awards = []
awards << earn(Badge.find(:conditions => { :name => "Recipeador" })) if user.recipes.count > 10
awards << earn(Badge.find(:conditions => { :name => "Answering Machine" })) if user.answers.valids.count > 1000 # an example
# I would also change the finds with some id (constant) for speedup
awards
end
end
我很抱歉,但是flash散列在模型中是不可访问的,它是在控制器中处理请求时创建的。您仍然可以使用实现您的方法来存储属于用户的badge对象中包含的badge infos flash消息:
class Badge
# columns:
# t.string :name
# seed datas:
# Badge.create(:name => "Recipeador", :description => "Posted 10 recipes")
# Badge.create(:name => "Answering Machine", :description => "Answered 1k questions")
end
class User
#...
has_many :badges
def earn_badges
awards = []
awards << earn(Badge.find(:conditions => { :name => "Recipeador" })) if user.recipes.count > 10
awards << earn(Badge.find(:conditions => { :name => "Answering Machine" })) if user.answers.valids.count > 1000 # an example
# I would also change the finds with some id (constant) for speedup
awards
end
end