【问题标题】:Updating model attributes in Ruby在 Ruby 中更新模型属性
【发布时间】:2015-06-07 19:35:14
【问题描述】:

我正在尝试让用户将传入好友请求的状态从“未接受”更改为“已接受”

用户模型

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :places


has_many :sent_friendships, class_name: "Friendship" , foreign_key: :sender_id
has_many :received_friendships, class_name: "Friendship", foreign_key: :receiver_id

友谊模型

class Friendship < ActiveRecord::Base
belongs_to :sender, class_name: "User"
belongs_to :receiver, class_name: "User"
end

用户显示视图

    <h1>Incoming Friend Requests</h1>
<% current_user.received_friendships.each do |friendship| %>
<%= friendship.sender.name %>
<%= link_to "Accept", user_accept_path(current_user), class: 'btn btn-success', method: :post %>
<% end %>

友谊控制器

 class FriendshipsController < ApplicationController

def accept
@user = User.find(params[:user_id])



end

end

我在哪里磕磕绊绊,因为我不确定如何最好地加上发送者 ID 和接收者 ID 以更新友谊模型表上的正确行。

感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    由于您的每个控制器都可以访问current_user 变量,因此您可能不会将其作为参数传递,但您应该将friendship.sender 传递给它。

    然后根据您的路线和架构,您可以执行以下操作之一:

    1. 建立新的友谊

      类 FriendshipsController

      def accept
          @user = User.find(params[:id])
          friendship.create sender_id: current_user.id, reciever_id: @user.id
          # or
          # friendship.create sender_id: current_user.id, reciever_id: params[:id]
      
          # then rdictect to somewhere
          redirect ...
      end
      

      结束

    2. 如果您在友谊模型中有其他字段,例如 - 接受,您可以更新。然后你会在发送友谊的过程中找到一个创建的友谊模型 邀请并更新字段。

      类 FriendshipsController

      def accept
          @user = User.find(params[:id])
          friendship = Frienship.where("sender_id = ? and reciever_id = ?",
                                   # sender was passed as an argument to link_to in the view
                                      @user.id, current_user.id)
          friendship.accept = true
          friendship.save!
      
      
          # then rdictect to somewhere
          redirect ...
      end
      

      结束

    【讨论】:

      【解决方案2】:

      您的accept 方法基本上应该做一组事情:

      1. 获取一个接受请求的用户(您应该已经为您完成了这项工作,因为 devise 在控制器中可以访问 current_user 辅助方法)
      2. 获取对应的received_friendship 以了解哪个特定用户将成为current_user 的新朋友
      3. 更新friendship 对象(来自步骤#2),将acceptance(或您命名的任何名称)属性设置为accepted

      如您所知,第一步不需要您进行任何编码。第二个应该像根据友谊请求的sender_id(或特定friendshipid,这会更好)设置一个本地(或@instance)变量一样简单。最后一个看起来像

      friendship.acceptance = "accepted"
      if friendship.save
        # TODO: redirect to somewhere with a successful notice
      else
        # TODO: render users/show template once again with an error
      end
      

      另一个提示:请注意,您的接受链接未传递第 2 步所需的数据:

      <%= link_to "Accept", user_accept_path(current_user) %>
      

      【讨论】:

      • 我感觉我的链接不正确。 user_accept_path(friendship.sender.id) 会是我需要的吗?
      • 是的。它将提供有关您要接受的人的数据。 current_user 随处可见。
      • 根据您的决定,这可以是friendship.sender_idfriendship.id 本身。我的选择是后一种选择。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多