问题是您将方法指定为 URL 查询参数,而不是作为link_to
方法的选项。
这是您实现所需目标的一种方法:
<%= link_to "Add as friend", user_friend_path(current_user, friend), method: 'put' %>
# or more simply:
<%= link_to "Add as friend", [current_user, friend], method: 'put' %>
link_to
使用帮助器更新模型属性的另一种方法是传递查询参数。例如:
<%= link_to "Accept friend request", friend_request_path(friend_request, friend_request: { status: 'accepted' }), method: 'patch' %>
# or more simply:
<%= link_to "Accept friend request", [friend_request, { friend_request: { status: 'accepted' }}], method: 'patch' %>
这将提出这样的请求:
Started PATCH "/friend_requests/123?friend_request%5Bstatus%5D=accepted"
Processing by FriendRequestsController#update as
Parameters: {"friend_request"=>{"status"=>"accepted"}, "id"=>"123"}
您可以在这样的控制器操作中处理:
def update
@friend_request = current_user.friend_requests.find(params[:id])
@friend_request.update(params.require(:friend_request).permit(:status))
redirect_to friend_requests_path
end