Javascript 将变量值从js函数传递到rails视图(动态选择)

Javascript 将变量值从js函数传递到rails视图(动态选择),javascript,jquery,ruby-on-rails,ruby-on-rails-3,parameter-passing,Javascript,Jquery,Ruby On Rails,Ruby On Rails 3,Parameter Passing,我使用jQuery通过JavaScript动态添加和删除嵌套模型字段 共有4种模式:客户机、c_人;项目,p_人。一对多的关联:客户有很多c_人,项目有很多p_人,这些人来自客户方,但他们属于项目 因此,我有以下问题: 在视图中使用helper方法link_to_add_字段时,需要传递另一个参数current_client_id,该参数取决于当前在选择框中选择的客户端 <!-- new.html.erb --> <p> <%= f.label :client_id

我使用jQuery通过JavaScript动态添加和删除嵌套模型字段

共有4种模式:客户机、c_人;项目,p_人。一对多的关联:客户有很多c_人,项目有很多p_人,这些人来自客户方,但他们属于项目

因此,我有以下问题:

在视图中使用helper方法link_to_add_字段时,需要传递另一个参数current_client_id,该参数取决于当前在选择框中选择的客户端

<!-- new.html.erb -->
<p>
<%= f.label :client_id %></br>
<%= f.collection_select :client_id, Client.order("title"), :id, :title %>
</p>
<p>
<%= f.fields_for :p_people do |builder| %>
<%= render "p_person_fields", :f => builder %>
<% end %>
</p>
<p><%= link_to_add_fields "Add Person", f, :p_people, current_client_id %></p>
要最终部分使用它,请执行以下操作:

<!-- _p_person_fields.html.erb -->
<div class="fields">
<p>
<%= f.collection_select :content, @people, :name, :name %>
<%= f.hidden_field :_destroy %>
<%= link_to_function "Remove", "remove_fields(this)" %>
</p>
</div>

我感谢你的帮助!也许有更好的解决方案?

我不喜欢的一件事是,您在一个真正属于您的控制器的视图助手中使用逻辑


也许更好的方法是使用当前的客户端id对控制器进行AJAX调用。然后,该控制器可以获取@people对象并发送回一个js.erb模板,您可以在其中向视图添加部分。或者它可以返回一个JSON对象,AJAX调用的处理程序将创建新的元素添加到DOM中。

我找到了更好的方法来为项目分配人员–使用多个选择选项。这允许不使用嵌套的模型字段

1.Use有两个模型:c_person和project,这两个模型之间有很多关联

2.创建联接表:

class CreateCPeopleProjects < ActiveRecord::Migration
  def self.up
    create_table :c_people_projects, :id => false do |t|
      t.references :project
      t.references :c_person  
    end  
  end

  def self.down
    drop_table :c_people_projects
  end
end
5.将方法添加到projects\u controller以动态更改人员的集合。和一行更新操作,用于更新nil对象

  def update
    @project = Project.find(params[:id])
    params[:project][:c_person_ids] ||= []

    respond_to do |format|
      if @project.update_attributes(params[:project])
        format.html { redirect_to(@project, :notice => 'Project was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update_people
    @update_people = CPerson.where(:client_id => params[:id]).order(:name) unless params[:id].blank?
    render :partial => "c_people"
  end
6.不要忘记路由:match/projects/update\u people/:id=>projectsupdate\u people

如果您使用的是Rails 3,那么您应该在application\u helper.rb中删除h:

link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))

<!-- new.html.erb -->
<p>
<%= f.label :client_id, "Client" %><br />
<%= f.collection_select :client_id, @clients, :id, :title %>
</p>
<div id="people">
<%= render "c_people" %>
</div>

<!-- _c_people.html.erb -->
<p>
<%= fields_for :project do |f| %>
 <% if @update_people.blank? %>
 <%= f.collection_select :c_person_ids, @people, :id, :name, {:selected => @project.c_person_ids}, {:multiple => true, :name => "project[c_person_ids][]"} %>
 <% else %>
 <%= f.collection_select :c_person_ids, @update_people, :id, :name, {}, {:multiple => true, :name => "project[c_person_ids][]"} %>
 <% end %>
<% end %>
</p>
jQuery(function($) {
 $(function() {
  $("#project_client_id").change(function() {
  var client_id = $("select#project_client_id :selected").val();
  if (client_id == "") {client_id = "0";}
  $.get("/projects/update_people/" + client_id, function(data){
   $("#people").html(data);
  })
  return false;
  });
 });
})
  def update
    @project = Project.find(params[:id])
    params[:project][:c_person_ids] ||= []

    respond_to do |format|
      if @project.update_attributes(params[:project])
        format.html { redirect_to(@project, :notice => 'Project was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update_people
    @update_people = CPerson.where(:client_id => params[:id]).order(:name) unless params[:id].blank?
    render :partial => "c_people"
  end
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")