title: Passing variable value form js function to rails view (dynamic select)
body: <p>I use  <a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">Ryan's method</a> to add and remove nested model fields dynamically through JavaScript using jQuery. </p>
  
  <p>There are 4 models: client, c_person; project, p_person. And one-to-many associations: client has many c_people and project has many p_people (these are people from client's side but they belongs to project).</p>
  
  <p>So, I have following problem:</p>
  
  <p>When I use helper method "link_to_add_fields" in my view, I need to pass another parameter (current_client_id) that depends on what client is currently selected in select box.</p>
  
  <pre><code>&lt;!-- new.html.erb --&gt;
  &lt;p&gt;
  &lt;%= f.label :client_id %&gt;&lt;/br&gt;
  &lt;%= f.collection_select :client_id, Client.order("title"), :id, :title %&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;%= f.fields_for :p_people do |builder| %&gt;
  &lt;%= render "p_person_fields", :f =&gt; builder %&gt;
  &lt;% end %&gt;
  &lt;/p&gt;
  &lt;p&gt;&lt;%= link_to_add_fields "Add Person", f, :p_people, current_client_id %&gt;&lt;/p&gt;
  </code></pre>
  
  <p>I need that to dynamically change the collection for @people variable in helper method,</p>
  
  <pre><code># application_helper.rb
  def link_to_add_fields(name, f, association, current_client_id)
      new_object = f.object.class.reflect_on_association(association).klass.new
      @people = CPerson.where(:client_id =&gt; current_client_id).order(:name) unless current_client_id.blank?
      fields = f.fields_for(association, new_object, :child_index =&gt; "new_#{association}") do |builder|
        render(association.to_s.singularize + "_fields", :f =&gt; builder)
      end
      link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
  end
  </code></pre>
  
  <p>to finally use it in partial:</p>
  
  <pre><code>&lt;!-- _p_person_fields.html.erb --&gt;
  &lt;div class="fields"&gt;
  &lt;p&gt;
  &lt;%= f.collection_select :content, @people, :name, :name %&gt;
  &lt;%= f.hidden_field :_destroy %&gt;
  &lt;%= link_to_function "Remove", "remove_fields(this)" %&gt;
  &lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is simple js function to get current_client_id value, but I don't know how to pass it to my view or helper method.</p>
  
  <pre><code>// application.js
  function current_client_id() {
  var current_client_id = $("select#client_id :selected").val();
  }
  </code></pre>
  
  <p>I appreciate your help! Maybe there is a better solution?</p>