使用外键关联显示回形针图像
display a paperclip image using Foreign key association
我有两个模特。我想在我的 resources_game 模型中使用外键 (resource_id) 来显示图像。
这是我的Resource
模特
class Resource < ActiveRecord::Base
has_many :resource_books
has_many :resource_games, :dependent => :destroy
belongs_to :resouce_type
has_attached_file :image, :styles => { :small => "100x120>"},
:url => "/assets/games/images/:basename/:style/:basename.:extension",
:path =>":rails_root/public/assets/games/images/:basename/:style/:basename.:extension"
accepts_nested_attributes_for :resource_books, :resource_games
这是我的ResourceGame
模特
class ResourceGame < ActiveRecord::Base
belongs_to :resource, :class_name => ResourceGame, :foreign_key => :resource_id
attr_accessor :add_resources
has_attached_file :image, :styles => { :small => "100x120>"},
:url => "/assets/games/images/:basename/:style/:basename.:extension",
:path =>":rails_root/public/assets/games/images/:basename/:style/:basename.:extension"
accepts_nested_attributes_for :add_resource
如果图像存储在资源 table 中,我会像这样检索并填充 table:
在我的 Resources
控制器中:
@latest_resources = Resource.paginate(:page => params[:page],:order => "id DESC", :per_page => 10)
那么在我看来:
<% @latest_resources.each do |project| %>
<tr>
<td><%= image_tag project.image.url(:small) %></td>
<td><%= project.resouce_type_name %></td>
<td><%= project.name %></td>
<td><%= project.url %></td>
</tr>
<% end %>
图像正在存储在resources_game
table中,所以我应该如何在我的视图中写入image_tag
来检索它?
您需要使用 form_for
并构建嵌套表单。这是 instructions,查看它然后 ping 我。
@resource.resourcegames
是一个数组,因为你有一个 has_many
关系,所以当你使用 form_for
时,你可以有 1 个嵌套形式或许多嵌套形式。
Usage
The basics of Paperclip are quite simple: Declare that your model has an attachment with the has_attached_file method, and give it a name.
Paperclip will wrap up to four attributes (all prefixed with that attachment's name, so you can have multiple attachments per model if you wish) and give them a friendly front end. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
By default, only _file_name is required for Paperclip to operate. You'll need to add _content_type in case you want to use content type validation.
More information about the options passed to has_attached_file is available in the documentation of Paperclip::ClassMethods.
以下链接有几个指南
Rails + Paperclip - Update/Edit multiple images
https://github.com/thoughtbot/paperclip/issues/1734
https://groups.google.com/forum/#!topic/rubyonrails-talk/Waxw8D6seRc
https://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
通过将 resource_games 模型添加到控制器中的数组,我找到了一个适合我的解决方案。
@latest_resources = Resource.find(:all, :include => :resource_games)
并像这样在视图中调用:
<% @latest_resources.each do |project| %>
<tr>
<% project.resource_games.each do |resourcegames| %>
<td><%= image_tag resourcegames.image.url(:small) %></td>
<% end %>
<td><%= project.resouce_type_name %></td>
通过这种方式,我可以同时使用来自多个 table 的数据填充 table。
我有两个模特。我想在我的 resources_game 模型中使用外键 (resource_id) 来显示图像。
这是我的Resource
模特
class Resource < ActiveRecord::Base
has_many :resource_books
has_many :resource_games, :dependent => :destroy
belongs_to :resouce_type
has_attached_file :image, :styles => { :small => "100x120>"},
:url => "/assets/games/images/:basename/:style/:basename.:extension",
:path =>":rails_root/public/assets/games/images/:basename/:style/:basename.:extension"
accepts_nested_attributes_for :resource_books, :resource_games
这是我的ResourceGame
模特
class ResourceGame < ActiveRecord::Base
belongs_to :resource, :class_name => ResourceGame, :foreign_key => :resource_id
attr_accessor :add_resources
has_attached_file :image, :styles => { :small => "100x120>"},
:url => "/assets/games/images/:basename/:style/:basename.:extension",
:path =>":rails_root/public/assets/games/images/:basename/:style/:basename.:extension"
accepts_nested_attributes_for :add_resource
如果图像存储在资源 table 中,我会像这样检索并填充 table:
在我的 Resources
控制器中:
@latest_resources = Resource.paginate(:page => params[:page],:order => "id DESC", :per_page => 10)
那么在我看来:
<% @latest_resources.each do |project| %>
<tr>
<td><%= image_tag project.image.url(:small) %></td>
<td><%= project.resouce_type_name %></td>
<td><%= project.name %></td>
<td><%= project.url %></td>
</tr>
<% end %>
图像正在存储在resources_game
table中,所以我应该如何在我的视图中写入image_tag
来检索它?
您需要使用 form_for
并构建嵌套表单。这是 instructions,查看它然后 ping 我。
@resource.resourcegames
是一个数组,因为你有一个 has_many
关系,所以当你使用 form_for
时,你可以有 1 个嵌套形式或许多嵌套形式。
Usage
The basics of Paperclip are quite simple: Declare that your model has an attachment with the has_attached_file method, and give it a name.
Paperclip will wrap up to four attributes (all prefixed with that attachment's name, so you can have multiple attachments per model if you wish) and give them a friendly front end. These attributes are:
<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at
By default, only _file_name is required for Paperclip to operate. You'll need to add _content_type in case you want to use content type validation.
More information about the options passed to has_attached_file is available in the documentation of Paperclip::ClassMethods.
以下链接有几个指南
Rails + Paperclip - Update/Edit multiple images
https://github.com/thoughtbot/paperclip/issues/1734
https://groups.google.com/forum/#!topic/rubyonrails-talk/Waxw8D6seRc
https://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
通过将 resource_games 模型添加到控制器中的数组,我找到了一个适合我的解决方案。
@latest_resources = Resource.find(:all, :include => :resource_games)
并像这样在视图中调用:
<% @latest_resources.each do |project| %>
<tr>
<% project.resource_games.each do |resourcegames| %>
<td><%= image_tag resourcegames.image.url(:small) %></td>
<% end %>
<td><%= project.resouce_type_name %></td>
通过这种方式,我可以同时使用来自多个 table 的数据填充 table。