Rails Nested Forms With Images

后端 未结 2 1650
日久生厌
日久生厌 2021-02-08 02:34

So I\'m building a nested form with an Campaigns model and a Sites model where Campaigns has_many Sites. In my Campaigns form I have:

<%= f.fields_for :sites          


        
相关标签:
2条回答
  • 2021-02-08 03:06

    From the form helper object (in your case builder and f) you should be able to access the model object and the url your looking for like this:

    <%= image_tag f.object.image_url(:thumb) %>
    

    The image in image_url depends on how you named the attribute, but for your sample this should be correct.

    0 讨论(0)
  • 2021-02-08 03:20

    You may want to investigate the paperclip gem: https://github.com/thoughtbot/paperclip

    It lets you do this to show a thumbnail of the existing picture.

    <div class="field">
        <% if @thing.logo? %>
        <%= image_tag @thing.logo(:thumb) %><br/>
          Change
        <% end %>
        <%= f.label :logo %>
        <%= f.file_field :logo %>
      </div>
    
    0 讨论(0)
提交回复
热议问题