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

后端 未结 2 1981
你的背包
你的背包 2021-01-27 15:01

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 control

2条回答
  •  独厮守ぢ
    2021-01-27 15:29

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

提交回复
热议问题