ishwr
ishwr

Reputation: 745

How to call helper method through select_tag

I am trying to call helper method from select_tag. I tried this:

<%= select_tag(:themes, options_for_select({"Black" => "compiled/styles", "Green" => "compiled/styles2"})) %>

When I put alert(this.options[this.selectedIndex].value) instead of calling method, it works. How can I change my code to call the method as desired?

EDIT

I have this on my application.js

$(document).ready(function() {
   $('#display_themes').change(function(){
       $.ajax({url: '<%= url_for :controller => 'application', :action => 'set_themes()', :id => 'this.value' %>',
               data: 'selected=' + this.value,
               dataType: 'script'
       }) 
   })
});

And in controller I have set_themes methode

def set_themes()
      @themes = params[:id]
      respond_to do |format|
        redirect_to '/galleries'
        format.html # index.html.erb
        format.xml  { render :xml => @themes }
    end
end

The problem now is @themes still empty even I change the dropdown

Routes.rb

match '', :controller => 'application', :action => 'set_themes()'

Upvotes: 0

Views: 364

Answers (1)

Sun
Sun

Reputation: 777

To route to application#set_themes:

match '/set_themes', to: 'application#set_themes', as: :set_themes

With that done, just direct the url in ajax as follows:

$.ajax({url: "<%= set_themes_path %>",

It should be @themes = params[:selected] in your controller based on what you are trying to do in application.js.

Upvotes: 1

Related Questions