Take the 2-minute tour ×

I have a view that lists all the states of US based on a certain condition. In the same view I have included an SVG map which picks up color based on the result set of states. The state list partial is mapped to a method A in my controller, while the JavaScript map is linked to a different method B in the same controller. However my method B needs to get the returned state lists from A. How can I share the variable between the two methods? I tried a before_filter method to load up some instance variables, but that didn't work.

Any help will be appreciated.


def reports
    @query   = @report.search_query
    @results = @query.results(:page => params[:page], :results_per_page => 200).hits
    @state = Array.new
    @results.each do |r|
        @state << r.result.state.abbreviation
    @finalstate = @state.uniq 

def data
    state_groups = State.select('abbreviation,premium').inject({}) do |groups, state|
        groups["#{state.abbreviation}"] = {
            :abbreviation => state.abbreviation,
            :group => @finalstate.include?(state.abbreviation) ? "found" : "not",
            :clickable => "true"
    render :json => state_groups.to_json

In my partials I have something like this:

content_for :footer_scripts do = javascript_bundle :bundle_report

My JavaScript map involves the data method to get the JSON. This JSON returns a null, and that is where I am stuck.

share|improve this question
What exactly did you try? Can you paste any code? That would help people answer your question. Its quite difficult to give you any advice without some more information. –  freedrull Feb 23 '12 at 23:38
In my controller I have 2 methods, reports which returns a list of bills. I am using Solr to do this search function, and here I can find an unique array of states selected by this query. This method is rendered via a partial "results.haml". The other method is data, which returns the same state list that I have found in results as a json object. The method data is invoked via an ajax call of a javascript that i also render in the same view. Does that explain it some ? Thanks –  user1227092 Feb 24 '12 at 3:42
Thanks Jackie for editing the code ! –  user1227092 Mar 1 '12 at 16:06
add comment


Your Answer



Browse other questions tagged or ask your own question.