Leo
Leo

Reputation: 2103

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...

Upvotes: 1

Views: 1073

Answers (3)

Umer Iqbal
Umer Iqbal

Reputation: 41

<%= 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 %>

Upvotes: 0

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

Try this:

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

If you want just a link see this

Upvotes: 1

Richard Peck
Richard Peck

Reputation: 76774

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() } );
    }
});

Upvotes: 0

Related Questions