【问题标题】:Ruby on rails 3 friendshipRuby on Rails 3 友谊
【发布时间】:2012-08-22 07:23:43
【问题描述】:

我正在尝试通过以下链接建立一个友谊系统:How to Implement a Friendship Model in Rails 3 for a Social Networking Application?。但是代码似乎非常过时,我可能会更改它,我正在尝试做的 atm 只是创建一个关系,但这似乎不起作用。

所以这里是我的创作

  #Send a friendship request
  def create
    Friendship.request(@customer, @friend)
    redirect_to friendships_path
  end

从技术上讲,这将调用位于模型中的方法请求,该模型已在上一篇文章中实现。

  def self.request(customer, friend)
    unless customer == friend or Friendship.exists?(customer, friend)
      transaction do
        create(:customer => customer, :friend => friend, :status => 'pending')
        create(:customer => friend, :friend => customer, :status => 'requested')
      end
    end
  end

我也将这些添加到模型中

attr_accessible :status, :customer_id, :friend_id, :customer, :friend

然而,友谊并没有建立起来。有什么理由不呢?我称关系已关注

<%= link_to "Add friend", friendships_path(:friend_id => customer), :method => :post %>

【问题讨论】:

  • 我正在尝试自己实现一个友谊模型,但我对应该在 attr_accessible 中添加什么感到困惑。我认为状态应该是那里的唯一属性。是否存在恶意用户更改 id 从而改变用户之间友谊的风险?

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1


【解决方案1】:

您需要将@customer 和@friend 分开。在您的链接中,您将 :friend_id 设置为客户,而您从不设置 @customer id。

试试这个:

def create
  @customer = current_account
  @friend = Account.find(params[:friend_id])
  Friendship.request(@customer, @friend)
  redirect
end

在你需要的link_to中:

<%= link_to "Add Friend", friendships_path(:friend_id => friend),: method => :post %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2012-07-09
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多