executor21
executor21

Reputation: 4586

How do you disable a select tag upon choosing in a rails web app?

I would like to present a select menu and, when the user chooses an option, disable that menu (preserving the selection).

My code is as follows:

my_controller.rb:

def action 
    @menu_disabled = true
end

my_helper.rb:

def menu_tag
   select_tag :menu_name,
           options_from_collection_for_select(@menu_options, 'id', 'title'),
           :prompt => 'my prompt',
           :disabled => @ menu_disabled
end

_menu.erb:

<%= menu_tag %>

index.html.erb:

<!-- headers, description, etc -->
<%= render :partial => 'menu' %>
<!-- footers, etc, -->

<script>
$(document).ready(function() {
    $('#menu_name').change(function() {
      $.ajax({
        url: "<%= action_path %>",
        data: {
          selection_id : $('#menu_name').val()
        },
        dataType: "script"
      });

    });
  });
</script>

action.js.erb:

$('#menu_name').html("<%= escape_javascript(render :partial => 'menu') %>");

This doesn't work. The menu remains functional, and displays the prompt upon selection. Other changes (for example, modifying the @menu_options collection in action) are displayed correctly. What's happening?

Rails version: 4.0.2

Ruby version: 2.0.0

Jquery-rails gem version: 3.1.2

Browser: Safari 8.0.4

Upvotes: 0

Views: 205

Answers (1)

RPinel
RPinel

Reputation: 896

Use replaceWith instead of html since you want to replace the whole select element.

Upvotes: 1

Related Questions