Question

I have a form that is non-responsive for some reason and I'm stuck!

I have followed the RailsCast here but it's based on an older version of rails.

Here's my view code, pretty sure that's where my trouble is:

<%= form_tag publish_selected_posts_path do %>
    <% @posts_inactive.each do |post| %>
      <tr>
        <td><%= check_box_tag "post_ids[]", post.id %></td>
        <td><%= link_to post.title, post_path(post) %></td>
        <td><%= post.created_at.to_s(:short) %></td>
        <td><%= post.user.email %></td>
        <td><%= post.user.email %></td>
        <td><%= post.state.name %></td>
        <td><%= post.city.name %></td>
        <td><%= post.expire_date.to_s(:short) %></td>
        <td>
            <div>
                <div class="dark" style="float:left;">
                  <%= link_to 'Edit', edit_post_path(post), :class => 'btn btn-mini dark' %> &nbsp;
                </div>
                <div style="float:left;">
                  <%= button_to 'Delete', post_path(post), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
                </div>
                <div class="dark" style="float:left;">
                     &nbsp; <%= link_to publish_link_text(post), 
                                toggle_publish_post_path(post), 
                                :class => 'btn btn-mini dark' %>
                </div>
                <div style="clear:both;">
                </div>
            </div>
        </td>
      </tr>
    <% end %>   

     </tbody>
    </table>
  <%= submit_tag "Publish Selected", :class => 'btn btn-mini dark' %>
<% end %>   

When I press the submit button, nothing is happening... As best I can tell, everything is as it should be based on the rails cast, but it's just not working.

Here's the HTML this form outputs:

    <form accept-charset="UTF-8" action="/dashboard/posts" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div>
          <tr>
            <td><input id="post_ids_" name="post_ids[]" type="checkbox" value="29" /></td>
            <td><a href="/posts/29">Another Post</a></td>
            <td>30 Nov 05:03</td>
            <td>guitarjoe02@email.com</td>
            <td>guitarjoe02@email.com</td>
            <td>Alaska</td>
            <td>Anchorage</td>
            <td>28 Jan</td>
            <td>
                <div>
                    <div class="dark" style="float:left;">
                      <a href="/posts/29/edit" class="btn btn-mini dark">Edit</a> &nbsp;
                    </div>
                    <div style="float:left;">
                      <form action="/posts/29" class="button_to" method="post"><div><input name="_method" type="hidden" value="delete" /><input class="btn btn-mini btn-danger" data-confirm="Are you sure?" type="submit" value="Delete" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div></form>
                    </div>
                    <div class="dark" style="float:left;">
                         &nbsp; <a href="/posts/29/toggle_publish" class="btn btn-mini dark">Publish</a>
                    </div>
                    <div style="clear:both;">
                    </div>
                </div>
            </td>
          </tr>
          <tr>
            <td><input id="post_ids_" name="post_ids[]" type="checkbox" value="28" /></td>
            <td><a href="/posts/28">New Post...</a></td>
            <td>16 Nov 01:09</td>
            <td>guitarjoe02@email.com</td>
            <td>guitarjoe02@email.com</td>
            <td>Alaska</td>
            <td>Anchorage</td>
            <td>14 Jan</td>
            <td>
                <div>
                    <div class="dark" style="float:left;">
                      <a href="/posts/28/edit" class="btn btn-mini dark">Edit</a> &nbsp;
                    </div>
                    <div style="float:left;">
                      <form action="/posts/28" class="button_to" method="post"><div><input name="_method" type="hidden" value="delete" /><input class="btn btn-mini btn-danger" data-confirm="Are you sure?" type="submit" value="Delete" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div></form>
                    </div>
                    <div class="dark" style="float:left;">
                         &nbsp; <a href="/posts/28/toggle_publish" class="btn btn-mini dark">Publish</a>
                    </div>
                    <div style="clear:both;">
                    </div>
                </div>
            </td>
          </tr>

  </tbody>
</table>
  <input name="commit" type="submit" value="Save changes" /> 
</form> 

Any ideas?

Was it helpful?

Solution

Ah ha! The culprit is your button_to calls. Check out the documentation for button_to. As you will see, that helper wraps itself in a mini HTML form. In your case, its creating a form within a form, which is invalid.

You should be able to do the exact same action using link_to instead. So your button_to will end up looking like this:

<%= link_to 'Delete', post_path(post), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>

Try that and let me know if it makes a difference.

OTHER TIPS

Your submit tag doesn't seem to be inside the form. That will make it not actually submit the form.

Edit: I think I might be wrong... but the dodgy indentation of your code makes it hard to tell. Can you clean up the indentation to make do/ends (and the code they contain) clearer and easy to see at a glance?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top