1

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

Leo
  • 2,061
  • 4
  • 30
  • 58

3 Answers3

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

Community
  • 1
  • 1
zishe
  • 10,665
  • 12
  • 64
  • 103
0

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() } );
    }
});
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

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

Umer Iqbal
  • 31
  • 4
  • i want to say, just don't show the button but button will always be there like <% f.submit %> instead of <%= f.submit %> – Umer Iqbal Apr 16 '22 at 11:20
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '22 at 15:25