访问从在一个看法的不同的看法的图象与在路轨的回形针宝石红宝石 [英] Access image from different view in a view with paperclip gem ruby on rails
问题描述
我是Ruby on Rails的新人,并且学习它。我想在另一个视图中访问一个包含由paperclip gem存储的图像的表,例如在我的应用程序中,我有原因控制器,我可以访问视图中的图像原因存储在表中的这个代码:
I'm new in Ruby on Rails and learning it. I want to access a table with images stored by paperclip gem in another view, for example in my application, I have the causes controller, I can access the image in the view causes stored in the table by this code:
=image_tag @cause.images.first.image.url(:thumb),
但我也有访问,从配置文件控制器存储在表中的图像。那么,如何在视图中访问视图的对象?我尝试原因控制器:
But i have access too, the images stored in the table from a profile controller. So, how do i access the object of view Profiles in the view Causes? I try in the causes controller:
-> @profile = Profile.all -> =image_tag @profile.images.first.image.url(:thumb),
所以朋友们,我该如何解决这个问题?感谢。
but not work, so friends, how do i resolve this problem? Thanks.
推荐答案
首先,在原因控制器中,复制 @profile
因为 Profile.all
将返回所有配置文件的数组。即将 @profile = Profile.all
更改为 @profiles = Profile.all
Firstly, in the causes controller, pluralize @profile
because Profile.all
will return an array of all profiles. i.e. change @profile = Profile.all
to @profiles = Profile.all
因为 @profiles
是一个数组,您需要遍历视图中的每个数组项目原因:
Because @profiles
is an array, you need to iterate through each array item in the view Causes:
<% @profiles.each do |profile| %>
<%= image_tag profile.images.first.image.url(:thumb) %>
<% end %>
如果您只打算返回一个配置文件映像,那么您需要在控制器中指定哪个配置文件。即
If you only intend on returning a single profile image then you will need to specify which profile in the controller. i.e.
@profile = Profile.first
或如果原因模型属于配置文件模型:
or if the cause model belongs to the profile model:
@profile = Profile.find(params[:profile_id])
这篇关于访问从在一个看法的不同的看法的图象与在路轨的回形针宝石红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!