简体   繁体   English

从带有回形针宝石红宝石的视图中访问不同视图中的图像

[英]Access image from different view in a view with paperclip gem ruby on rails

I'm new in Ruby on Rails and learning it. 我是Ruby on Rails的新手,正在学习它。 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: 我想在另一个视图中访问带有回形针gem存储的图像的表,例如,在我的应用程序中,我有Causes控制器,我可以通过以下代码访问存储在表中的视图原因中的图像:

 =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? 那么,如何在View Causes中访问View Profiles的对象? 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. 谢谢。

You are sending Profile.all into @profile that means @profile is going to be an array of profile objects. 您要将Profile.all发送到@profile中,这意味着@profile将是一组配置文件对象。 your method images will work on one object of Profile class, not on multiple. 您的方法图像将对Profile类的一个对象起作用,而不对多个对象起作用。 You need to select the proper profile and assign it to @profile. 您需要选择适当的配置文件并将其分配给@profile。 For EX : 对于EX:

@profile = Profile.first # just taking the first profile, you can select any.

In view , now you can use this @ profile to get an image. 在view中,现在您可以使用此@配置文件获取图像。

Firstly, in the causes controller, pluralize @profile because Profile.all will return an array of all profiles. 首先,在原因控制器中,将@profile复数,因为Profile.all将返回所有配置文件的数组。 ie change @profile = Profile.all to @profiles = Profile.all @profile = Profile.all更改为@profiles = Profile.all

Because @profiles is an array, you need to iterate through each array item in the view Causes: 因为@profiles是一个数组,所以您需要遍历视图中的每个数组项。原因:

<% @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. 如果仅打算返回单个配置文件映像,则需要在控制器中指定哪个配置文件。 ie

@profile = Profile.first

or if the cause model belongs to the profile model: 或原因模型属于概要模型:

@profile = Profile.find(params[:profile_id])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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