【问题标题】:Array syntax in JSON in rails 3 on HerokuHeroku上rails 3中JSON中的数组语法
【发布时间】:2011-12-11 23:49:26
【问题描述】:

我有以下控制器代码:

  def index
    @profiles = Profile.where("(first_name || ' ' || last_name) ILIKE ?", "%#{params[:q]}%")

    @autolist = []
    @profiles.each do |profile|
      user = User.find_by_id(profile.user_id)
      @autolist.concat([{"id",profile.id,"name",profile.first_name+" "+profile.last_name,"email",user.email}])
    end    

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @autolist }
      end
  end

它可以在我的本地环境中运行,但会使我的应用程序崩溃。特别是这一行:@autolist.concat([{"id",profile.id,"name",profile.first_name+" "+profile.last_name,"email",user.email}])

有什么想法吗?

我感觉这与我使用 ruby​​ 1.8.7 和运行 1.9.2 的 heroku 应用程序的本地环境有关

【问题讨论】:

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


    【解决方案1】:

    这适用于 1.8.7:

    >> h = {"id", 6}
    => {"id"=>6}
    

    但不在 1.9.2 中:

    >>  h = {"id",6}
    SyntaxError: (irb):4: syntax error, unexpected ',', expecting tASSOC
     h = {"id",6}
               ^
        from /Users/mu/Developer/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
    

    火箭符号会更好地为您服务:

    @autolist.concat([{ "id" => profile.id, "name" => profile.first_name + " " + profile.last_name, "email" => user.email}])
    

    我在 1.9.1 或 1.9.2 发行说明中找不到任何提及此更改的内容,这实际上是我第一次看到 Ruby 哈希的 {'a', b} 语法。也许这种表示法是一个被弃用的功能,最终消失了。

    顺便说一句,在 1.8.7 上开发并在 1.9.2 上部署并不是最好的主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 2019-02-04
      • 2012-05-16
      • 1970-01-01
      • 2011-09-26
      • 2013-06-01
      相关资源
      最近更新 更多