1

我有一个模型用户,一个用户可以是 ruby​​ on rails 3 中的管理员

用户表包括

  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 => ""

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

4

1 回答 1

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>

这能满足您的需要吗?

于 2012-05-25T19:58:46.977 回答