srlrs20020 srlrs20020 - 1 year ago 29
Ruby Question

Why doesn't submitting this form raise an error?

Hi I am wondering why I am not getting an error when I submit a form with an action set to a method that isn't set up yet. After submission it just keeps me on the same page with different url parameters.

Here is the form:

<%= form_tag( drop_piece_path, :method => "post" ) do %>

<% 7.times do |col| %>
<%= label_tag col %>
<%= radio_button_tag(:column, col) %>
<% end %>

<%= submit_tag("Enter move")%>
<% end %>

Here is my route:

post 'drop_piece' => 'connect_four#drop_piece', as: :drop_piece

And here is my drop_piece method in my controller:

def drop_piece
redirect_to fake_path #should raise an error because fake_path is not a real path

I am confused because since my form is set to submit to the drop_piece_path shouldn't that trigger the drop_piece method in my controller? Which should then raise an error?

Again, right now after form submission the application just stays on the same page with params corresponding to whichever radio button I selected.

Why doesn't this trigger my drop_piece method in my connect_four controller? Thanks for the help.

Answer Source

This happens because you have nested form tags. One is <form> tag and another one is <%= form_tag... %>. HTML spec doesn't allow nesting forms, so the outer one is submitted. Since it doesn't have action attribute, it is submitted to the current URL.