1

I have the following written:

<% if @document != nil %>
<%= form_for(@document, :html => { :multipart => true }) do |f| %>
  <% if @document.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@document.errors.count, "error") %> prohibited this document from being saved:</h2>

  <ul>
  <% @document.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>
<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>
<%= render partial: 'documents/resume' %>
<div id="add_buttons">
  <%= f.file_field :resume %>
  <%= f.submit %>
</div>
<% end %>
<% else %>
<h1>No Document Available</h1>
<% end %>

Sorry about the indenting, copy/paste didn't paste it right. Anyways, this is using paperclip and works fine to upload a document as long as in the resume partial that I have listed I have my delete button commented out. Here is the code for that partial:

<div class='Resume_1'>
  <%#= @current_user = session[:user] %>
  <%#= @document = @current_user[:document] %>
  Current Resume: 
  <%= render partial: 'documents/resumelink' %>
</div>
<div class='Resume_2'>
  <% if (@document != nil) && (@document.resume_file_name != nil) %>
    <%= button_to "Delete Resume", @document, method: :delete, data: { confirm: 'Are you sure you wish to delete your resume?' } %>
  <% end %>
</div>

If the button_to line is commented out, the update document (f.submit) button works fine. However, if the button_to line is left in, the f.submit button does nothing. And here's the destroy function (which is where the delete method is routed to) since this doesn't actually seem to remove the resume either.

  def destroy
    @document.resume = nil
    respond_to do |format|
      format.html { redirect_to welcome_url, notice: 'Resume was successfully deleted.' }
      format.json { head :no_content }
    end
  end

Thanks for any help.

1
  • The button_to still does a submit for your form. You can only have one submit button.
    – Daiku
    Commented Jul 31, 2014 at 15:50

1 Answer 1

5

If you look at docs. The html generated by button_to helper is

<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
#      <div><input value="New" type="submit" /></div>
#    </form>"

So when you have a button_to inside a form you basically have a form inside a form and hence your submit button doesn't work.

Fix:

Instead of using a button_to use link_to helper and give bootstrap classes or some custom class to make it look like a button

<%= link_to "Delete Resume", @document, class: "btn btn-default" method: :delete, data: { confirm: 'Are you sure you wish to delete your resume?' } %>
4
  • K, was hoping to avoid that. Do you know of a way to delete the file that was uploaded? = nil doesn't do that and nor did .destroy, .clear Commented Jul 31, 2014 at 19:13
  • @DanielCamburn just delete it like a normal record and if you are not using any external service to store it then it would be stored in your public folder.
    – Mandeep
    Commented Aug 1, 2014 at 8:54
  • Thank you Mandeep. The solution I ended up using was to end my form and put the button_to after the form. It worked with formatting because the submit button and file_field were aligned to the right so the button_to actually showed up to the right of the link where I wanted it. To delete, I apparently had to do document.save and that fixed the delete issue. Thank you though! Commented Aug 4, 2014 at 15:57
  • @DanielCamburn what was the problem with using link_to?
    – Mandeep
    Commented Aug 4, 2014 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.