Search code examples
javascriptruby-on-railspostgresqlhstore

Rails option tags in text_field_tag confusing


I found this code in a github repository, at https://github.com/heroku/hstore_example . Many thanks to Richard Schneeman for the only example I've found anywhere that shows syntax, in Railspeak, for accepting user input for hstore data using html.erb. Everybody else just shows how it can be used in the console (I don't think my users will be firing up a command line window any time soon).

I have two questions: 1, why is the word "key" repeated twice in the 5th line? And 2, at the bottom, after the anchor tag, it says herf="#". Is it safe to assume that's a typo, and he meant "href?" The link doesn't lead anywhere, and the class is a JS selector used by the script below the code listed here. Is it misspelled on purpose, to prevent an action?

 <%= f.fields_for :data, @product.data do |d| %>
    <% @product.data.try(:each) do |key, value| %>
        <div class="row">
          <p class='span3'>
            <%= text_field_tag key, key, :class => 'text_field dynamicAttributeName' %>
      </p>
       <p class='span3'>
        <%= d.text_field key, :class => 'text_field', :value => value %>
      </p>
      <p class='span1'>
        <a herf='#' class='btn removeRow'>X</a>
      </p>
    </div>
<% end %>
<%- end -%>

Also in this app (same repo), we find this code:

<%= form_tag hstore_queries_path, :method => :get, :remote => true, :id => 'hstore_query' do %>

<p>Find:</p>
<%= select_tag 'query_type', "<option value='key_eql'>key equal to ?  </option>
                            <option value='key_value_eql'>key equal to value</option>
                            <option value='key_not_value'>key not equal to value</option>
                            <option value='key_like_value'>key like value</option>".html_safe %>

<%= text_field_tag   :key   , nil, :placeholder => 'Key' %>
<%= text_field_tag   :value , nil, :placeholder => 'Value', :class => 'hide' %>

<br />
<%= submit_tag "Run Query", :class => 'btn btn-primary' %>

<%- end -%>

it's from a partial used to create a search tool for the db. Below the select box code, there's another text_field tag--this time the word :key is expressed as a symbol. I can see that in the upper chunk of code, the key is an iterator variable (though I don't know why it's there twice, and I don't know which of them refers to the iterator). What I don't see, in the lower chunk, is any reference to the model, "products," or database field, "data. " It's the only hstore in the db table, but wouldn't it at least need to be referenced somewhere? :Key isn't a field in itself, nor is :value--sorry I'm new to this, but how does Rails know what db field is being referred to? It's nowhere in the partial, nor is it passed through the render command that triggers it.


Solution

  • 1, why is the word "key" repeated twice in the 5th line?

    That's passing the value of the variable key into the first two parameters of text_field_tag. Those are the name and value parameters. The key variable is defined on line 2.

    http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag

    2, at the bottom, after the anchor tag, it says herf="#". Is it safe to assume that's a typo, and he meant "href?"

    Yes, that's just a typo.