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

淺唱寂寞╮ 提交于 2019-12-02 09:31:36

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

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])

You are sending Profile.all into @profile that means @profile is going to be an array of profile objects. your method images will work on one object of Profile class, not on multiple. You need to select the proper profile and assign it to @profile. For 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!