将URL从回形针返回到JSON

用户名

我有一个包含CMS系统的rails应用程序,该系统用于将景点从城市输入到数据库。我正在使用回形针将图像上传到亚马逊s3一切正常。现在,我想让ios应用程序使用的json文件包含s3中上传的图像的url 我在这里看到了一些答案,但似乎无法使我的代码正常工作。我有这个。

放置模型

attr_accessible :assets_attributes, :asset
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true

def avatar_url
   assets.map(&:asset_url)
end

资产模型

class Asset < ActiveRecord::Base
  attr_accessible :asset_content_type, :asset_file_name, :asset_file_size, :asset_updated_at, :place_id, :asset
  belongs_to :place
  has_attached_file :asset

   validates_attachment :asset, :presence => true,
  :content_type => { :content_type => ['image/jpeg', 'image/png'] },
  :size => { :in => 0..1.megabytes }

def asset_url
    asset.url(:original)
    end

end

查看代码

<%= f.fields_for :assets do |asset_fields| %>

<% if asset_fields.object.new_record? %>
<p>
    <%= asset_fields.file_field :asset %>
</p>
<% end %>

<% end %>
<br/>

<%= f.fields_for :assets do |asset_fields| %>

<% unless asset_fields.object.new_record? %>


<%= link_to image_tag(asset_fields.object.asset.url(:original), :class => "style_image"), (asset_fields.object.asset.url(:original)) %>
    <%= asset_fields.check_box :_destroy %>

<% end %>

<% end %>

位置控制器

def index
    @places = Place.all
render :json => @places.to_json(:methods => [:avatar_url])
  end

谁能帮帮我吗?

理查德·派克

关于链接到的SO问题(如何在to_json中获取回形针图像的url),需要一些元素才能使图像正确呈现

您遇到的问题是Paperclip的image方法实际上是一个ActiveRecord对象,因此,您不能不做其他事情就只能在JSON请求中呈现它


_url方法

该过程中最重要的部分是在asset模型中定义“ _url”方法这基本上调用了.urlPaperclip函数,允许JSON快速创建图像的所需URL(图像的URL不是ActiveRecord对象,因此可以通过JSON发送)

根据所引用的SO问题,您应该将此操作放入模型中:

#app/models/asset.rb
def asset_url
    asset.url(:medium)
end

现在,当您在控制器中呈现JSON请求时,可以使用以下类型的设置:

#app/controllers/places_controller.rb
render :json => @places.to_json(:methods => [:asset_url])

由于您的asset模型是的关联places,因此可能无法立即生效。但是,这绝对是正确的方向,因为我记得自己做的这件事

这里要注意的重要一点是,您实际上是通过JSON传递图像的裸露“ URL”,而不是图像对象本身


更新资料

这是我们制作的视频会议演示应用程序中的一个示例:

#app/controllers/profiles_controller.rb
def update
   @profile = User.find(current_user.id)
   @profile.profile.update(upload_params)

   respond_to do |format|
      format.html { render :nothing => true }
      format.js   { render :partial => 'profiles/update.js' }
      format.json { render :json => @profile.profile.as_json(:only => [:id, :avatar], :methods => [:avatar_url])
      }
    end
end

   #app/models/profile.rb
   def avatar_url
       avatar.url(:original)
   end

因此,对于您来说,我会尝试这样做:

def index
    @places = Place.all
    render :json => @places.assets.as_json(:only => [:id, :asset], :methods => [:asset_url])
end

您也可以尝试如下操作:

#app/models/profile.rb
def avatar_url
   self.asset.avatar.url(:original)
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将JSON返回到GET请求

来自分类Dev

从php文件将json返回到ajax

来自分类Dev

从Action类将json返回到ajax

来自分类Dev

Paypal 按钮将 url 返回到 WordPress 并显示付款信息

来自分类Dev

如何将JSON结果返回到Ajax.BeginForm

来自分类Dev

angularjs:将json数据从服务返回到控制器

来自分类Dev

如何使用Java Servlet将JSON对象返回到AngularJS

来自分类Dev

使用jQuery将JSON值返回到表单字段

来自分类Dev

$ .get将json_encoded php数组返回到javascript

来自分类Dev

将格式化的日期从WebMethod返回到JSON

来自分类Dev

如何将JSON结果返回到Ajax.BeginForm

来自分类Dev

如何将JSON数据从python返回到javascript?

来自分类Dev

将输入返回到段落

来自分类Dev

将具有HTTP URL的showmodaldialog的返回值返回到HTTPS页面

来自分类Dev

将Json值从Android发布到服务器,并使用SQL将数据返回到Android

来自分类Dev

如何将JSON响应返回到Rails应用json.parse意外令牌

来自分类Dev

将Arraylist从Servlet返回到DAO,再返回到Servlet,然后返回到Jsp

来自分类Dev

git push后如何将URL返回到git repo?

来自分类Dev

MVC,无法返回到先前的URL

来自分类Dev

Typescript Angular 2如何将JSON数据从服务返回到组件?

来自分类Dev

通过AJAX将JSON对象从Django视图返回到客户端

来自分类Dev

Django使用自定义SQL而不是模型将JSON对象返回到模板

来自分类Dev

将嵌套的JSON从PostgreSQL的多对多联接表返回到node.js

来自分类Dev

Django:将结果从两个mysql表返回到JSON

来自分类Dev

在Swift 2中,如何将JSON解析错误返回到完成块?

来自分类Dev

升级后,原始sql查询将json字段作为字符串返回到postgres

来自分类Dev

将JSON的一部分返回到对象Spring Boot

来自分类Dev

在不使用JSON的情况下将数组从PHP返回到JavaScript

来自分类Dev

将JSON结果返回到Ajax请求的MVC ErrorHandling属性仅在localhost上有效

Related 相关文章

  1. 1

    将JSON返回到GET请求

  2. 2

    从php文件将json返回到ajax

  3. 3

    从Action类将json返回到ajax

  4. 4

    Paypal 按钮将 url 返回到 WordPress 并显示付款信息

  5. 5

    如何将JSON结果返回到Ajax.BeginForm

  6. 6

    angularjs:将json数据从服务返回到控制器

  7. 7

    如何使用Java Servlet将JSON对象返回到AngularJS

  8. 8

    使用jQuery将JSON值返回到表单字段

  9. 9

    $ .get将json_encoded php数组返回到javascript

  10. 10

    将格式化的日期从WebMethod返回到JSON

  11. 11

    如何将JSON结果返回到Ajax.BeginForm

  12. 12

    如何将JSON数据从python返回到javascript?

  13. 13

    将输入返回到段落

  14. 14

    将具有HTTP URL的showmodaldialog的返回值返回到HTTPS页面

  15. 15

    将Json值从Android发布到服务器,并使用SQL将数据返回到Android

  16. 16

    如何将JSON响应返回到Rails应用json.parse意外令牌

  17. 17

    将Arraylist从Servlet返回到DAO,再返回到Servlet,然后返回到Jsp

  18. 18

    git push后如何将URL返回到git repo?

  19. 19

    MVC,无法返回到先前的URL

  20. 20

    Typescript Angular 2如何将JSON数据从服务返回到组件?

  21. 21

    通过AJAX将JSON对象从Django视图返回到客户端

  22. 22

    Django使用自定义SQL而不是模型将JSON对象返回到模板

  23. 23

    将嵌套的JSON从PostgreSQL的多对多联接表返回到node.js

  24. 24

    Django:将结果从两个mysql表返回到JSON

  25. 25

    在Swift 2中,如何将JSON解析错误返回到完成块?

  26. 26

    升级后,原始sql查询将json字段作为字符串返回到postgres

  27. 27

    将JSON的一部分返回到对象Spring Boot

  28. 28

    在不使用JSON的情况下将数组从PHP返回到JavaScript

  29. 29

    将JSON结果返回到Ajax请求的MVC ErrorHandling属性仅在localhost上有效

热门标签

归档