Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6376673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:46:30+00:00 2026-05-25T01:46:30+00:00

Assume I have a vanilla form containing a selection list and a submit button:

  • 0

Assume I have a vanilla form containing a selection list and a submit button:

<%= form_for(@report :url => report_path) do |f| %>
  <%= f.select(:sort_by, options_for_select(Report.SORT_OPTIONS, @report.sort_by)) %>
  <%= f.submit 'sort' %>
<% end %>

Works fine, except that the user has to first make a selection and then click ‘sort’. What I’d really like is to have the action triggered when the user makes a selection and to ditch the ‘sort’ button altogether. This feels like a perfect use case for unobtrusive jQuery.

So [based on responses from @Dennis and @ethan below], before this form_for I added:

<%= javascript_include_tag(:defaults) %>

<script type="text/javascript">
$(document).ready(function() { 
  var $select = $('#report_sort_by'),
      $form = $select.closest('form');
  // $('[type="submit"]', $form).attr('type', 'hidden'); // gives error
  $('[type="submit"]', $form).hide();                    // works
  $select.change(function() { $form.submit(); });
});
</script>  

So is it considered right to plunk javascript right into the .html.erb file? Or is it better to put this in a separate .js file, and if so, what are the conventions for paramaterizing and naming the .js file?

For a few reputation points, can someone show me and all the other tyros how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:46:31+00:00Added an answer on May 25, 2026 at 1:46 am

    Rails will stringify the object & property you’re using, so the select menu would have id="report_sort_by". Which means you could use:

    var $select = $('#report_sort_by'),
        $form = $select.closest('form');
    
    // preserve the button's name/value attributes, if Rails has assigned them
    $('[type="submit"]', $form).hide();
    
    $select.change(function() {
      $form.submit();
    });
    

    If you want to include this code in a robust, unobtrusive way, you could make a file called maybe MyProject.Report.js and put this code in it:

    if (!MyProject) var MyProject = {};
    
    MyProject.Report = {
      init: function() {
        var $select = $('#report_sort_by'),
            $form = $select.closest('form');
    
        // preserve the button's name/value attributes, if Rails has assigned them
        $('[type="submit"]', $form).hide();
    
        $select.change(function() {
          $form.submit();
        });
      }
    }
    

    Then in your .erb file (at the bottom, after the form) you could add <script type="text/javascript">MyProject.Report.init();</script> and then, of course, add the javascript_include_tag to include the JS file itself.

    There’s a bunch of different ways you could do your namespacing & encapsulation. The MyProject.Report object that I describe above has the chief advantage of being incredibly straightforward. As your code grows, you just add new functionality inside the MyProject.Report object like:

    MyProject.Report = {
      init: function(options) {
        // some event handlers, maybe
        this.complication(null, null, options.data);
        this.reusable(options.bgcolor);
      },
    
      complication: function(a, b, c) {
        // stuff that's tough to look at
        this.reusable(b);
      },
    
      reusable: function(q) {
        // code that's used multiple places
      }
    }
    

    And then you basically hack away at that until you’re knowledgeable enough to decide whether you need a framework like backbone.js for a given project 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker
Assume I have generic list L of some type in c#. Then, using linq,
Assume I have a List<SomeObject> a . Now also assume that I have another
Assume i have control button with name button1 and function with name doSomething. Function
Assume I have the following query: Update LPMBonusReport Set BoxID = (Select ContainerSerialNumber From
Assume I have a class foo, and wish to use a std::map to store
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume you have some objects which have several fields they can be compared by:
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.