Rails 4引发ArgumentError(错误的参数数量(2表示1)):通过AJAX POST更新时

时间:2017-09-19 08:38:53

标签: jquery ruby-on-rails ruby ruby-on-rails-4 rails-api

每当我希望用户点击应用的通知下拉列表时,我想将通知的status更新为read

这是控制器代码:

class Api::V1::NotificationsController < ApiController
  before_action :authenticate_user!

  def index
    @notifications = Notification.where(user_id: current_user.id).order(created_at: :desc)

    render_success @notifications
  end

  def create
    Notification.update_all({status: "read"}, {user_id: current_user.id})
  end

end

以及应该为控制器触发create方法的jQuery代码:

$('.notifications-menu').on('shown.bs.dropdown', function () {
  console.log('Opened notifications');
  $.post('/api/v1/notifications');
})

不幸的是,它会返回此错误:

Started POST "/api/v1/notifications" for ::1 at 2017-09-19 16:27:53 +0800
Processing by Api::V1::NotificationsController#create as */*
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 13]]
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.6ms)

ArgumentError (wrong number of arguments (2 for 1)):
  app/controllers/api/v1/notifications_controller.rb:11:in `create'

1 个答案:

答案 0 :(得分:0)

create方法更改为:

def create
  Notification.update_all(status: "read", user_id: current_user.id)
end