【问题标题】:Rails passing paramsRails 传递参数
【发布时间】:2011-06-12 20:53:17
【问题描述】:

我觉得我错过了一种从根本上更简单的方法;无论哪种方式,我似乎都没有弄清楚数组的语法。试图把东西塞进 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])

谢谢。

【问题讨论】:

  • :mentee, :mentor, :status
  • 因为你应该使用哈希,而不是数组
  • 你为什么要和params开玩笑?
  • @apneadiving 抱歉,我还在学习语法 - 这看起来像哈希?
  • @monadic 我正在尝试发送 Match.new 三个值,并且我假设(不准确,我在想?)我需要将它们放入 params 数组中。有没有更简单的方法来做到这一点?

标签: ruby-on-rails arrays params


【解决方案1】:
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

数组不组织数据(除非你认为数组中的位置是已知且有意义的)

EDIT2:

并纠正这一点:

@match = Match.new(array_of_match_information)

EDIT3:

我鼓励你看看http://railsforzombies.org,看来你需要一个好的教程。

实际上,在学习时构建应用程序可能很危险,因为当您不了解基本架构时,最终会过度编码无法维护的代码。

例如,您的线路:

    array_of_match_information[:mentor] = self.id

看起来很奇怪。

【讨论】:

  • 嗯。好吧,这确实有效(即,没有抛出异常),谢谢 - 但它没有像我预期的那样将值传递给 Match.new。
  • 我认为我的控制器代码会选择这个:def new @match = Match.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @match } end end
  • 太棒了。感谢您的耐心等待 - 这种语法很难学习,来自 php - 效果很好。
  • 你真的不需要 array_of_match_information 哈希。只需用 {} 包装它并将其传递给方法:Match.new({ :mentor => ..., :mentee => ..., ...})
  • @apneadiving 这个应用程序并不重要(它是一种爱好,而不是客户)——我认为给自己分配一些事情是最好的学习方式 :)
【解决方案2】:

看来,您正在尝试实现一个基本的社交网络功能。如果我是对的,您应该使用关联。它看起来像这样(我不知道你的导师和导师关系的细节,所以我想这是一个多对多的关系):

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

但正如我所说,这只是一个代码示例。我不能保证它会工作或适合您的应用程序。

你完全应该去看看this book

【讨论】:

    【解决方案3】:

    我不能 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"
    

    没有看到你的模型,我很难知道,但我怀疑它实际上需要哈希,而不是数组。

    【讨论】:

    • 当然更接近 :) 但我仍然得到这个异常:Symbol as array index
    • 哦,是的,你需要一个哈希。我的错。我没看错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2014-03-03
    相关资源
    最近更新 更多