0

I have a controller "mainpage", with a correspondingly named view. The controller creates a @myLocalSuites variable, and the view includes the following line:

<li class="active"><%= link_to "Perforce", :action =>  'renderp4', :remote => true, :localSuites => @myLocalSuites %></a></li>

Routing is defined such that clicking this link_to calls renderp4.js.erb to render a partial within the mainpage view:

$('#MainPage').replaceWith('<%= escape_javascript render "perforce_sync/perforceSync" %>')

where _perforceSync partial includes:

<%= select_tag "perforceSuites", options_for_select(*MYOPTIONSVARIABLE*), {:class => 'form-control', :size => "20", :multiple => true} %>

Where *MYOPTIONSVARIABLE* needs to be myLocalSuites as cascaded down from the mainpage view/controller.

Having tried everything I can think of, and failed - can someone please show how to modify the above snippets to use the required variable in the PerforceSync partial? Everything I've tried seems to produce something along the lines of:

 ActionView::Template::Error (undefined method `map' for nil:NilClass):

An example of what I've tried. I don't think I'm a million miles off, but...

<li class="active"><%= link_to "Perforce", :action =>  'renderp4', :remote => true, :localSuites => @myLocalSuites %></a></li>

$('#MainPage').replaceWith('<%= escape_javascript render "perforce_sync/perforceSync", :suitesLocally => params[:localSuites]%>')

<%= select_tag "perforceSuites", options_for_select(params[:suitesLocally]), {:class => 'form-control', :size => "20", :multiple => true} %>

Thanks! :)

Steve Hall
  • 469
  • 1
  • 5
  • 23
  • Let me see if I have this right, you want the link to call the javascript function `$('#MainPage').replaceWith()`, and then dynamically generate the argument to this function with some data based on a select tag on the page? This will require ajax, since you will have to send the selected option(s) to the server, then use that response in your javascript code. – Slicedpan Mar 26 '14 at 11:10
  • Ummm not quite (I don't think at least!)... The argument in question (an array of strings - @myLocalSuites) exists in the parent "mainpage" view. I want to pass this to the replaceWith, and then pass it on from there to the partial that gets rendered within mainpage, to populate the select_tag with the contents of that array... – Steve Hall Mar 26 '14 at 11:20
  • why not just `options_for_select(@myLocalSuites)`? – Slicedpan Mar 26 '14 at 11:37
  • That's what I had expected to work - but for some reason, the value get's lost.... `<%= puts @myLocalSuites %>
  • <%= link_to "Perforce", :action => 'renderp4', :remote => true %>
  • ` in the mainpage view shows the value of @myLocalSuites correctly, yet if I repeat that "puts" in the perforceSync.erb.html, it's empty... – Steve Hall Mar 26 '14 at 11:47
  • Are you expecting the `perforceSync` partial to be rendered immediately on page load? – Slicedpan Mar 26 '14 at 12:06
  • No - I have a navbar at the top of the mainpage, and it is one of the links on this navbar that is the above `
  • `. So on mainpage load, the browser window is mostly empty bar the navbar, but on clicking the "Perforce" entry on the navbar, it does the replaceWith - replacing an empty `
    ` with the contents of the partial...
  • – Steve Hall Mar 26 '14 at 12:30
  • Ok, you do know that you can't execute ruby code (including partials) in a web page after the page has loaded. – Slicedpan Mar 26 '14 at 12:37
  • Hmmm.... so if prior to page load, @myLocalSuites has been populated with the Array of values... is there a logical / simple way to keep a copy of that array client-side (and pass this through to the partial for later rendering)? Or am I needing to look into AJAX (another new technology for me!). As you can undoubtedly tell, I'm fairly new here - learning as fast as I can! Im starting to get the impression that a button on my partial "refresh" - to update the select_tag on the partial is required - with this button firing off some AJAX to perform the behind the scenes fun, and get the array? – Steve Hall Mar 26 '14 at 12:52
  • gonna write an answer, give me a min – Slicedpan Mar 26 '14 at 12:55