Question

I have a controller with just one method:

index_controller.rb

def index
  # some code
end

The form:

index.html.erb

<%= form_tag :class => "form-inline signup" do %>
  <div class="form-group">
    <%= text_field_tag :url, nil, :class => "form-control", :placeholder => "URL do tópico" %>
  </div>
  <%= submit_tag "Enviar", method: :post, :class => 'btn btn-theme' %>
<% end %>

And a simple root route:

    root 'index#index'
    post '/', to: 'index#index'

The problem is that when I load the root page, the form is posted automatically, when the preferable was to POST just on the button call.

What am I missing here?

Was it helpful?

Solution

You should move the code for the post out to another action that can handle that.

post '/', :to => "index#submit"

Then you can define a submit action within your IndexController to handle the form, and the index action won't run the form code anymore.

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