Search code examples
ruby-on-railsruby-on-rails-3formsparametersactioncontroller

I'm getting a hash outputted to my view if I run params[:query], but if I run #values on it, it breaks and says "I'm a String"


So I'm trying to set the default search field value to the pre-existing one if it exists

<%= form_tag universities_path, :method => "get", :class => "form-search" do %>
  <%= label_tag :query, "Search by name" %>
  <%= text_field_tag :query, :input => params[:query] unless params[:query].nil? %>
  <%= text_field_tag :query if params[:query].nil? %>
  <%= label_tag :state  %>
  <%= select_tag :state, options_for_select(@states, params[:state])   %>
  <%= submit_tag "Search", :class => 'btn' %>
<% end %>

The above code outputs a hash that looks like this ({:input=>"coastal"}). When I run the x.values where x = params[:query] I get a "undefined method `values' for "coastal":String".

Running params[:query].class yields this (to the input field), btw ({:input=>String})


Solution

  • text_field_tag expects the second parameter as the value of the field. change

    <%= text_field_tag :query, :input => params[:query] unless params[:query].nil? %>
    <%= text_field_tag :query if params[:query].nil? %>
    

    to

    <%= text_field_tag :query, params[:query] %>