jamby
jamby

Reputation: 606

Rails form not going to correct action

So I've got a particular form that's supposed to be comparing two objects. One being the current page that they're on and the other is the one we're searching for.

Here's the form in the show.html.haml:

%form.form-inline
  = form_tag compare_search_path, method: 'get' do
    = text_field_tag :q, params[:q], class: "typeahead", "data-provide" => "typeahead", autocomplete: "off"
    = hidden_field_tag :search_item_id, '', class: "search_item_id"
    = hidden_field_tag :current_item, @item.id
    = submit_tag "Search", name: nil, class: "btn"

Inside my controller, I've got:

def search_related
  puts "*" * 100
  puts "It's in here and working."
end

However, it is never getting inside the method, it just attempts to do a search and the method is never getting used.

My routes.rb has:

get "/compare_search" => 'items#search_related'

The URL that it seems to try and go to is this: http://localhost:3000/items/1?utf8=%E2%9C%93&q=[ITEM IS HERE]&search_item_id=3&current_item=1

All of the parameters are correct, but it just doesn't seem to want to go to the correct action (all I want right now is for it to go into the action so I can do what I need in there).

Any idea what I'm doing wrong?

Upvotes: 1

Views: 78

Answers (2)

BillyMFH
BillyMFH

Reputation: 26

I'm thinking your route should be:

get "/compare_search", to: 'items#search_related', as: 'compare_search'

Upvotes: 0

nbucciarelli
nbucciarelli

Reputation: 460

You have 2 form elements in there.

%form.form-inline
  = form_tag compare_search_path, method: 'get' do

Remove the top line and make it:

  = form_tag compare_search_path, method: 'get', class: "form-inline" do

Upvotes: 1

Related Questions