【问题标题】:Cant change value of boolean read column in rails for notification system无法更改通知系统的rails中布尔读取列的值
【发布时间】:2015-05-22 05:53:29
【问题描述】:

我正在为我的应用实施通知系统。我创建了一个属于模型 User 的表通知(一个用户有很多通知)。表通知有列内容(字符串)和读取(布尔) 我有一个带有下拉菜单的导航栏,显示未读通知和一个脚本,该脚本假设通过点击时的 ajax 帖子将通知的已读列从 false 更改为 true。

<script>



$(document).ready(function() {
    $("#dropdownMenu1").on('click', function(event){
      $.ajax({url: "<%=update_notification_path%>",
          dataType: "json",
          type: "POST",
          success: function() {
                          $(".noti_bubble").hide();

                        },
      });
    });
 });

这个 ajax 导致我的通知控制器的操作如下所示:

def update_notif
    current_user.notifications.each do |notification|
      notification.read == true
      notification.save
    end
    render nothing: true
  end

但在那之后,当我检查 rails 控制台时,读取的布尔值没有任何改变。

我做错了什么?

【问题讨论】:

  • 您能描述一下update_notification_path 的定义位置吗?
  • 在配置/路由post "updatenotif", to: "notifications#update", as: "update_notification"

标签: ruby-on-rails ajax notifications


【解决方案1】:

请将notification.read == true 这样的方法更新为notification.read = true

def update_notif
   current_user.notifications.each do |notification|
     notification.read = true
     notification.save
   end
   render nothing: true
end

【讨论】:

  • 我发现了错误:我的路线是:“notifications#update”,而我的控制器操作是 update_notif !
  • == 检查两个操作数的值是否相等,如果是则条件为真
  • = 简单赋值运算符,将右侧操作数的值赋值给左侧操作数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 2017-04-10
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多