How to find the value of a select_tag using JQuery using Rails 4

I would like to find the value of a select_tag in Rails 4 using JQuery. Once I have the value I want to update the view with a partial containing radio buttons.

My code is not working. Can you let me know what I am doing wrong.

index.html.erb file

<%= form_tag experts_path, method: :get do %>
    <p>
      <%= text_field_tag :query, params[:query] %>
    <hr>

    <h3>Skills Categories</h3>
        <%= select_tag "skill_categories", options_from_collection_for_select(@skill_categories, "id", "category"), prompt: "All Skills" %>

    <div class="skills"></div>
<% end %>

<script>
$( "#skill_categories" ).change(function() {
    var category_from_select = $(".skills").find(":selected").text();
    $(".skills").replaceWith("<div class='skills'><%=j render 'experts/skills', selected_category: category_from_select %></div>");
});
</script>

_skills/html.erb partial

<h4>Skills</h4>
<% skill_all.each do |skill| %>
    <%= radio_button_tag "skills[]", "#{skill.name}",
                         ((params[:skills] != nil) && (params[:skills].index(skill.name) != nil) ? true : false) %>
    <%= skill.name %>
<% end %>

experts_controller.erb

  def skills_search
    @skills = Skill.all
  end

experts_helper.erb

module ExpertsHelper

  def skill_all
    @skills = Skill.all
  end
end

Answers


Try to change:

var category_from_select = $(".skills").find(":selected").text();

to:

var category_from_select = $(this).find(":selected").text();

To get selected option, you need select element with#skill_categories not div with .skills


Need Your Help

c++ deleting vector class member memory in destructor

c++ memory-management vector

I have a class containing a vector member variable.

mod_rewrite httpd.conf: Block access to directories on HTTP_REFERER

php regex apache mod-rewrite

I am (learning) implementing a Front Controller pattern, redirecting all requests to index.php and appending as a query string (to be parsed later by PHP) via a mod_rewrite rule in httpd.conf, belo...