如何通知管理员用户的生日是今天在ruby on rails

时间:2012-05-25 19:33:41

标签: ruby-on-rails

我有一个模型用户,一个用户可以在rails 3上的ruby中使用ADMIN

用户的表格包括

  t.boolean :admin,              :default => false
  t.string :email,              :null => false, :default => ""
  t.string :department
  t.string :username
  t.datetime :birthdate 
  t.string :encrypted_password, :null => false, :default => ""

我的问题是如何显示或通知管理员今天是用户的生日  如果可能有一个模型,控制器和视图包含在答案中,我会很高兴

1 个答案:

答案 0 :(得分:0)

好的,你已经有了模型,用户就是这样。我只想添加一个方法,告诉我们用户的生日是否是今天:

class User
  def birthday_today?
    birthday == Date.today?
  end
end

然后在控制器中,假设您要在索引页面上显示它。我假设你有自己的身份验证解决方案,所以让我们说对current_user的调用将使用现在登录的用户

class WelcomeController < ApplicationController
  def index
    @display_honourees = current_user.admin?
    @honourees = User.all.find_all {|u| u.birthday_today? }
  end
end

那里,我们拥有所需的所有数据。现在让我们展示它们:

<html>
  ...
  <% if @display_honourees %>
    <ul>
      <% @honourees.each do |honouree| %>
        <li>It's <%= honouree.username %>'s birthday today! Make a party!</li>
      <% end %>
    </ul>
  <% end %>
  ...
</html>

这可以满足您的需求吗?