简体   繁体   中英

RoR: JSON mapped :key => “value” – change all key names

This is what I have:

def index
    @attachments = current_user.attachments.all
    respond_to do |format|
      format.json do
        render :json => @attachments.map { |o| { url: o.picture.thumb.url }}
      end
    end
  end

=> [{: url =>"/uploads/attachment/picture/7/thumb_df3c0c3c.jpg"}, {: url =>"/uploads/attachment/picture/12/thumb_dd7839ee.jpg"}, ... }]

How can I change the key from :url to :thumb ?

=> [{:thumb=>"/uploads/attachment/picture/7/thumb_df3c0c3c.jpg"},
 {:thumb=>"/uploads/attachment/picture/12/thumb_dd7839ee.jpg"}, ... }]

This is the whole object after: render :json => @attachments

My goal: thumb: thumb: "/uploads/attach..."

嵌套对象json

Background: https://www.froala.com/wysiwyg-editor/docs/concepts/image-manager

I use the gem carrierwave to create a thumb

response.map! { |urls| { :thumb =>  urls[:url] }  }

将密钥从“ url”更改为“ thumb”

render :json => @attachments.map { |o| { **thumb: o.picture.thumb.url** }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM