使用轨道上的回形针 gem ruby 在视图中从不同视图访问图像 [英] 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])
这篇关于使用轨道上的回形针 gem ruby 在视图中从不同视图访问图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!