【问题标题】:How to write if statement in Rails 4 as_json method?如何在 Rails 4 as_json 方法中编写 if 语句?
【发布时间】:2015-04-28 20:45:56
【问题描述】:

我正在使用 Rails 4。我正在创建 API 数据库,用户可以在其中从 Facebook Graph API 注册。 如果用户没有头像,则 image_url 为空。

在阅读了 SO 中的答案后,我认为这是为我的响应构建自定义 json 的正确方法。

我创建了 as_json 方法来在创建用户时仅使用应该返回的参数来呈现响应。 这是我创建 json 响应的方法:

  def as_json(options={}){ 
        id: self.id,
        first_name: self.first_name,
        last_name: self.last_name,
        auth_token: self.auth_token,
        image: {
            thumb: "http://domain.com" + self.profile_image.thumb.url
        }
    }
    end

上面的这个方法给我一个错误:no implicit conversion of nil into String

如果图像存在于我的数据库中,我需要提供绝对图像 url 路径,但如果图像 url 在我的数据库中为空,我不需要提供此参数作为响应。 如何在这个 as_json 方法中编写 if 语句? 这个我试过了,还是不行。

  def as_json(options={}){ 
        id: self.id,
        first_name: self.first_name,
        last_name: self.last_name,
        auth_token: self.auth_token,
        if !self.profile_image.thumb.url == nil
            image: {
                thumb: "http://domain.com" + self.profile_image.thumb.url
            }
        end     
    }
    end

在 Jorge de los Santos 的帮助下,我设法通过以下代码通过 no implicit conversion of nil into String 错误:

def as_json(options={})
  response = {  id: self.id,
                first_name: self.first_name,
                last_name: self.last_name,
                auth_token: self.auth_token }
  if !self.profile_image.thumb.url == nil
    image = "http://domain.com" + self.profile_image.thumb.url
    response.merge(image: {thumb: image })
  end
  response
end

但是现在所有的用户都返回了没有图像参数,即使他有一个图像 url。

【问题讨论】:

    标签: ruby-on-rails json ruby-on-rails-4


    【解决方案1】:

    您的代码看起来不错,但当您尝试合并图像键时,合并功能无法按您预期的那样工作,请检查以下内容以了解:

     hash = {a: 1, b:2 }
     hash.merge(b: 3)
     puts hash   #{a: 1, b:2 }
     hash = hash.merge(b: 3)
     puts hash   #{a: 1, b:2, c: 3 }
    

    所以你需要通过改变这一行来修改你的代码:

    response.merge(image: {thumb: image })
    

    response = response.merge(image: {thumb: image })
    

    【讨论】:

      【解决方案2】:

      使用 Jbuilder

      当你构建一个复杂的 json 对象时,最好使用 jbuilder,我假设模型被称为 'User'

      创建一个名为show.json.jbuilder的模板

      json.id @user.id
      json.first_name @user.first_name
      json.last_name @user.last_name
      json.auth_token @user.auth_token
      unless @user.profile_image.thumb.url.nil?
        json.image do |image|
          image.thumb "http://domain.com#{@user.profile_image.thumb.url}"
        end
      end
      

      我建议为图像 url 创建一个帮助程序,这样我们就可以调用类似

      的方法
      json.image @user.full_profile_image_url
      


      使用 as_json

      至于您自己的方法(使用 as_json),您可以创建一个返回完整图像哈希的方法

      class User < ActiveRecord::Base
        def image
          { thumb: "http://domain.com#{profile_image.thumb.url}" }
        end
      end
      

      然后在as json中调用方法

      @user.to_json(
        only: %i(id first_name last_name auth_key),
        methods: :image
      )
      

      这将调用 image 方法并将其值设置在一个名为 'image' 的键中

      【讨论】:

        【解决方案3】:

        您不能在哈希键内使用逻辑,您可以在返回哈希之前声明变量,或者您可以在哈希值内使用完整语句。但我认为这更具可读性。

        def as_json(options={})
          response = {  id: self.id,
                        first_name: self.first_name,
                        last_name: self.last_name,
                        auth_token: self.auth_token }
          image = "http://domain.com" + self.profile_image.thumb.url
          response.merge!({image: {thumb: image }}) unless self.profile_image.thumb.url
          response
        end
        

        【讨论】:

        • 啊,你想要一个可选密钥!
        • 感谢您的回复。我试过了,但如果图像 url 为空,它就不起作用。我收到错误消息:no implicit conversion of nil into String 如果 url 为空,我将无法执行 "http://domain.com" + self.profile_image.thumb.url。但是如果用户在 Facebook 中没有个人资料图片,则此字段可以为空,因此我只想在数据库中存在图片 url 时才包含它。
        • 那是因为你将一个方法调用为 nil。你的问题中没有说明这一点。使用错误的堆栈跟踪更新它。
        • 我已经编辑了我的问题,并在我的进度中添加了一个带有更新的代码。
        • 您可以使用三元运算符...condition ? "output if true" : "output if false"
        猜你喜欢
        • 2016-06-28
        • 2012-06-22
        • 2011-12-15
        • 2021-10-13
        • 1970-01-01
        • 2018-08-01
        • 2010-10-29
        • 2023-03-24
        • 1970-01-01
        相关资源
        最近更新 更多