Rails 4 引发 ArgumentError(错误数量的参数(2 为 1)):通过 AJAX POST 更新时
Rails 4 raises ArgumentError (wrong number of arguments (2 for 1)): when updating through AJAX POST
我想在用户单击应用程序的通知下拉菜单时将通知的 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');
})
不幸的是,它 returns 这个错误:
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" = 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'
将您的 create
方法更改为:
def create
Notification.update_all(status: "read", user_id: current_user.id)
end
我想在用户单击应用程序的通知下拉菜单时将通知的 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');
})
不幸的是,它 returns 这个错误:
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" = 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'
将您的 create
方法更改为:
def create
Notification.update_all(status: "read", user_id: current_user.id)
end