Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在用户和朋友之间创建一个类似于rails应用中Facebook的挑战?_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 如何在用户和朋友之间创建一个类似于rails应用中Facebook的挑战?

Ruby on rails 如何在用户和朋友之间创建一个类似于rails应用中Facebook的挑战?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个rails应用程序,其中用户有朋友列表。现在我必须创建一个类似于facebook挑战的挑战,用户可以完成流程(玩游戏),他可以挑战他的朋友,他的朋友可以接受或拒绝请求,如果接受,在流程(玩游戏)完成后,必须向两个用户发送包含谁赢了的消息 我该怎么做?请帮帮我。你已经有了有很多:通过关系了吗 您需要将:source传递给users表,因为用户也可以是朋友。这看起来像这样: class User < ActiveRecord::Base has_many :friends has_

我有一个rails应用程序,其中用户有朋友列表。现在我必须创建一个类似于facebook挑战的挑战,用户可以完成流程(玩游戏),他可以挑战他的朋友,他的朋友可以接受或拒绝请求,如果接受,在流程(玩游戏)完成后,必须向两个用户发送包含谁赢了的消息


我该怎么做?请帮帮我。

你已经有了
有很多:通过
关系了吗

您需要将
:source
传递给users表,因为用户也可以是朋友。这看起来像这样:

class User < ActiveRecord::Base
 has_many :friends
 has_many :users, :source => :friend, :through => :friends
end

基于
关系\u状态
您可以解决您的问题

听起来你想要一款名为
Challenge
的新机型。这可能有几个关联:

class Challenge < ActiveRecord::Base
  belongs_to :sender, class_name: "User", inverse_of: :sent_challenges
  belongs_to :receiver, class_name: "User", inverse_of: :received_challenges
end
然后,您可能在
用户上有一个发送质询的方法

def send_challenge(friend)
  sent_challenges.create(receiver: friend)
end
您可能在
ChallengesController上有一些操作:

def index
  @challenges = current_user.received_challenges
end

def create
  @challenge = current_user.send_challenge(params[:friend_id])
  # now the sender plays the game
  render :game
end

def accept
  @challenge = current_user.received_challenges.find(params[:id])
  # now the receiver plays the game
  render :game
end

def deny
  current_user.received_challenges.destroy(params[:id])
  redirect_to challenges_url
end

def complete
  # happens at the end of the game
  # work out the winner
  # send the emails
end
当然,您需要添加相应的路由来连接所有内容,并为
索引和
游戏编写视图。也许你应该在你的朋友列表中添加指向
create
操作的链接,这样人们就可以提出挑战

请注意,我是如何让所有内容都通过
当前用户。收到的\u挑战
,而不仅仅是执行基本的
挑战。查找(params[:id])
-如果您这样做了,任何人都可以通过猜测id接受挑战!哎呀


我说了很多“也许”和“也许”,因为有不同的方法可以解决这个问题。但我希望这足以让你开始。如果没有,我建议尝试Rails教程-这是经典的。

这听起来并不难-您只需要创建UI来设置挑战,并在数据库中记录挑战的发送/接受/拒绝状态,并发送结果通知或电子邮件。你坚持哪一点?我已经在用户之间建立了友谊。我想向朋友发送挑战。我该怎么做?如果你已经获得了友谊,那么在friendship表中添加一个新列,如“challange_status”,如果用户向朋友发送了一个挑战,那么将该列设置为“pending”。我该如何像发送朋友请求一样向朋友发送挑战?然后,正如我在问题中所说,我必须比较结果(分数),然后显示谁赢了。这取决于你的代码(你没有发布代码,所以对我来说很难…)。只需找出要发送challange请求的位置,然后检查关系中的challange_状态栏即可。如果challange=挂起。。。。还有……非常感谢。。我只是在找这个。
def send_challenge(friend)
  sent_challenges.create(receiver: friend)
end
def index
  @challenges = current_user.received_challenges
end

def create
  @challenge = current_user.send_challenge(params[:friend_id])
  # now the sender plays the game
  render :game
end

def accept
  @challenge = current_user.received_challenges.find(params[:id])
  # now the receiver plays the game
  render :game
end

def deny
  current_user.received_challenges.destroy(params[:id])
  redirect_to challenges_url
end

def complete
  # happens at the end of the game
  # work out the winner
  # send the emails
end