滑轨通过参数

| 我觉得我缺少一种从根本上更容易做到的方式;无论哪种方式,我似乎都没有弄清楚数组的语法。试图将东西塞入params数组中。任何帮助表示赞赏。
@user = User.find(params[:user][:id])

array_of_match_information = Array.new
array_of_match_information[mentee] = @user.id
array_of_match_information[mentor] = self.id
array_of_match_information[status] = \"Pending\"    

@match = Match.new(params[:array_of_match_information])
谢谢。     
已邀请:
        
array_of_match_information = Hash.new
array_of_match_information[:mentee] = @user.id
array_of_match_information[:mentor] = self.id
array_of_match_information[:status] = \"Pending\"    
编辑
Hash
是键/值存储,就像您打算这样做一样。
mentee
是将与值
@user_id
相关的键 数组不组织数据(除非您认为数组中的位置已知且有意义) 编辑2: 并更正此:
@match = Match.new(array_of_match_information)
编辑3: 我鼓励您看一下http://railsforzombies.org,看来您需要一个很好的教程。 实际上,在学习时构建应用程序可能会很危险,因为当您不了解基本架构时,最终会过度编码无法维护的代码。 例如,您的行:
    array_of_match_information[:mentor] = self.id
看起来真的很奇怪。     
        看来,您正在尝试实现基本的社交网络功能。如果我是对的,则应使用关联。看起来像这样(我不知道您的导师与受训者关系的细节,所以我想这是一个多对多关系):
class User < ActiveRecord::Base
    has_many :matches
  has_many :mentors, :through => :match
  has_many :mentees, :through => :match
end

class Match < ActiveRecord::Base
  belong_to :mentor, :class_name => \'User\'
  belong_to :mentee, :class_name => \'User\'
end
然后,您可以在控制器中执行以下操作:
class matches_controller < ApplicationController

  def create
    # current_user is a Devise helper method 
    # which simply returns the current_user through sessions. 
    # You can do it yourself.

    Match.create({ :mentee => @user, :mentor => current_user }) 
    # \"pending\" status could be set up as a default value in your DB migration
  end

end
但是正如我所说,那只是代码示例。我不能保证它会起作用或适合您的应用程序。 而你完全应该看看这本书     
        我不确定要尝试做什么,但至少100%设置3个值时应该使用符号:
array_of_match_information[:mentee] = @user.id
array_of_match_information[:mentor] = self.id
array_of_match_information[:status] = \"Pending\" 
编辑: 您实际上应该这样做:
match_information = {}
match_information[:mentee] = @user.id
match_information[:mentor] = self.id
match_information[:status] = \"Pending\"
没有看到您的模型,我很难知道,但是我怀疑它实际上是需要哈希,而不是数组。     

要回复问题请先登录注册