'submitting a field with just enter button

How do I submit a form (it may be just one field, i will give example in a second) with just 'enter' button, without showing a submit button to the user, or even placing it in a code at all?

right now i have:

= form_tag admin_users_path, :method => 'get'
= text_field_tag :filter, params[:filter]

and after clicking 'enter' it sends me to the Admin::UsersController, but params[:filter] is blank, however my information is displayed by "better_errors" in QUESRY_STRING and REQUEST_URL. Any ideas how can i make this work?

EDIT

Solution WITHOUT form_tag would be very much appreciated, it keep screwing with my css...



Solution 1:[1]

Try this:

= form_tag admin_users_path, :method => 'get' do
  = text_field_tag :filter, params[:filter]

If you want just a link see this

Solution 2:[2]

Solution WITHOUT form_tag would be very much appreciated, it keep screwing with my css...

HTML sends data through forms, and if you wanted to submit a text_field with enter, you'll have to use a simple form to define both the submission path & which data to send

Therefore, the two ways you can use are either to use JS, or a form to submit the field:


form_tag

   = form_tag admin_users_path, :method => 'get'
       = text_field_tag :filter, params[:filter]

This will submit with enter, and to fix your css, just amend the styles to work with the form. If you're scrimping on this fundamental html functionality


JS

The other option will be to mimic the submission of an HTML form with javascript:

$('element').on('keyup', function(e) {
    if (e.which == 13) {
         $.post(url, { value1: $(this).val() } );
    }
});

Solution 3:[3]

<%= search_form_for @q do |f| %>

<%= f.search_field :title_cont, placeholder:"Search" %>

<%= f.submit %> <-------(1) <% end %>

you just need to remove the "=" sign form the arrowed line or line (1) like <% f.submit %>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Community
Solution 3 Umer Iqbal