Pergunta

I have a mongo rails 4 app, with an embedded photo. I have no problem in the new form, but in the edit form, if the user has not upload any photo, the form will not display a "add photo button".

Here is the form:

<%= f.fields_for :founder_profile_photos do |founder_photo_f| %>
  <%= render partial: 'founder_profile_photo_fields', locals: { f: founder_photo_f } %>
  <%= link_to_add_association raw('<i class="fi-plus">&nbsp; add a founder photo</i>'), f, :founder_profile_photos %>
<% end %>

Here is the partial:

<div class="nested-fields">
  <% if f.object.file.to_s.empty? %>
    <%= f.file_field :file, label: "Upload a founder photo." %>
  <% else %>
    <%= image_tag f.object.file, class: 'small-10 medium-10 image-previewer' %>
  <% end %>
</div>
Foi útil?

Solução

Thank you so much for your help. I finally found what was wrong, I needed to build the field photo in the edit action of my controller, other the field does not exist and therefore do not appear in the view.

def edit
  if @user.photos.empty?
    @user.photos.build
  end
end

photo is embedded into the user model (one to many relation) if you have a one to one relation you have to do:

if @user.photo.blank?
  @user.photo = Photo.new
end

Thank you very much for all your help, still so much to learn.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top