【问题标题】:Amount of information on Controller Method - Rails控制器方法的信息量 - Rails
【发布时间】:2013-07-25 20:45:59
【问题描述】:

我担心仪表板视图的控制器方法上显示的信息量。基本上,这个仪表板视图显示了我的应用程序中的用户信息和活动,但我认为这很快就会变成一团糟,因为传递给视图的信息量很大。

@answered = answered.length
@asked = asked.length
@pending = pending.length
@favorited = favorited.length
@medal_gold = thanks_received(:awesome).length
@medal_silver = thanks_received(:great).length
@medal_bronze = thanks_received(:good).length
@notification_pending = question_users_not_seen(pending, current_user.user_notification.pending_last_seen).count
@notification_silver = question_users_not_seen(thanks_received(:great), current_user.user_notification.silver_last_seen).count
@notification_gold = question_users_not_seen(thanks_received(:awesome), current_user.user_notification.gold_last_seen).count
@notification_bronze = question_users_not_seen(thanks_received(:good), current_user.user_notification.bronze_last_seen).count
@notification_asked = question_users_not_seen(asked, current_user.user_notification.asked_last_seen).any?
@notification_answered = question_users_not_seen(answered, current_user.user_notification.answered_last_seen).any?
@notification_favorited = question_users_not_seen(favorited, current_user.user_notification.favorited_last_seen).any?

有没有更优雅的方式来编写所有这些信息?我只关心代码的外观,而不是它的性能。

【问题讨论】:

  • 您可能希望在与这些值相关的模型上使用更多帮助程序或类方法..

标签: ruby-on-rails controller dashboard


【解决方案1】:

这将是使用哈希的好时机。例如,

@attr_counts = {:answered => answered.length, :asked => asked.length, :pending => pending.length, :favorited => favorited.length}
@medal_lengths = {:gold => thanks_received(:awesome).length, :silver => thanks_received(:great).length, :bronze => thanks_received(:good).length}

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多