Rails,正在更新邀请系统的记录

Rails, updating records for an invitation system

我想让用户接受邀请,接受标签在邀请模型中(所以我需要更新 table)。到目前为止,当用户点击接受按钮时没有任何反应

查看

 <% @invites.where(user_id: current_user.id).find_each do |invite| %>
...
 <%= button_to "Accept", accept_invite_invites_path(invite), method: :put %>
 end

路线

  resources :invites do
    collection do
      get 'accept_invite'
    end
  end

控制器

def accept_invite

  @invite = Invite.find(params[:id])
  @invite.accept
end

def decline_invite
  @invite = Invite.find(params[:id])
  @invite.decline
end

    def set_invites
      @invite = @story.invites.find(params[:id])
    end

 def new
    @invite = @story.invites.new
 end

如果我保留 :update 作为 set_invites 的一部分,我会得到 "undefined method `invites' for nil:NilClass",删除更新允许我的代码 运行,但不会对数据库进行任何更改。

型号

   def accept
    accept = true
    save

  end

  def decline
    accept = false
    save

  end

控制台

Processing by InvitesController#update as 
  Parameters: {"authenticity_token"=>"BDle9fqXHT9ZFctMbO4RvxfPuTQXe2Nq+b6/T29B3xjpYdtMozVUFLiRlaQFtuYzMrBceTQn8OtfGjJTe4wa/Q==", "id"=>"accept_invite"}
  User Load (1.7ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
No template found for InvitesController#update, rendering head :no_content
Completed 204 No Content in 85ms (ActiveRecord: 1.7ms)

这很奇怪,因为数据库是从用户 table 选择而不是更新邀请 table

那么问题是什么?路由有问题吗?我的 set_invites 方法?

So what is the problem? Is the route faulty? My set_invites method?

是的,您的路线有问题。如我所见,您在 collection 上声明了路线,但您需要在 member。你也应该把它改成 put.

resources :invites do
  member do
    put 'accept_invite'
  end
end