Ernesto G
Ernesto G

Reputation: 545

form_tag always redirecting to the same controller#action (static_pages#index)

This problem is driving me crazy.

I have two forms in application.html.erb. They are supposed to trigger pedidos#index and pedidos#excel actions, but instead static_pages#index is being triggered no matter what. I'm not getting any error message. The first form works if I manually write the url /pedidos and then I submit.

routes.rb

root 'static_pages#index'
get 'pedidos', to: 'pedidos#index'
get 'excel', to: 'pedidos#excel'

application.html.erb

<%=form_tag pedidos_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], placeholder: 'Búsqueda por OSCAR' %>
<%= submit_tag "Búsqueda", :name => nil, class: 'btn btn-info' %>
<% end %>

The other form:

<%=form_tag excel_path(format: :xlsx) , :method => 'get' do |f| %>
<%=select_date Date.today, prefix: :fecha %>
<%=submit_tag "Generar EXCEL de entregas", :name => nil, class: 'btn btn-info btn-sm' %>
<% end %>

And finally this is how the routes looks like:

pedidos_path    GET /pedidos(.:format)  pedidos#index

excel_path  GET /excel(.:format)    pedidos#excel

root_path   GET /   static_pages#index

Upvotes: 0

Views: 76

Answers (1)

Ernesto G
Ernesto G

Reputation: 545

Finally it turned out it was an HTML problem, I had the forms nested inside the Bootstrap navbar-form code.

This way it works:

<div class="form-group">
     <%= form_for '', url: {controller: 'pedidos', action: 'index' }, method: :get, class: 'navbar-form navbar-left' do |f| %>
     <%= text_field_tag :search, params[:search], placeholder: 'Búsqueda por OSCAR' %>
     <%= submit_tag "Búsqueda", :name => nil, class: 'btn btn-info' %>
     <% end %>
</div>

Upvotes: 1

Related Questions