0

我使用​​通过使用jQuery的JavaScript动态添加和删除嵌套的模型字段。将变量值形式的js函数传递给rails视图(动态选择)

有4种型号:client,c_person;项目,p_person。和一对多的关联:客户有很多c_people,项目有很多p_people(这些是来自客户端的人,但他们属于项目)。

所以,我有以下问题:

当我使用在我看来helper方法“link_to_add_fields”,我需要通过另一个参数(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> 

我需要动态改变的集合中的辅助方法@people变量,

# 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 => current_client_id).order(:name) unless current_client_id.blank? 
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| 
     render(association.to_s.singularize + "_fields", :f => builder) 
    end 
    link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")) 
end 

,终于用它在部分:

<!-- _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> 

下面是简单的JS功能获取current_client_id值,但我不知道如何将它传递给我的视图或帮助器方法。

// application.js 
function current_client_id() { 
var current_client_id = $("select#client_id :selected").val(); 
} 

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

回答

0

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

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

0

我发现了将人员分配给项目的更好方法 - 使用多个选择选项。这允许不使用嵌套模型字段。

1.使用has_and_belongs_to_many两个模型之间的关联:c_person和项目。

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 

3.修改new.html.erb和部分。

<!-- 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> 

4.添加js函数以获取当前客户端id值,将其发送到projects_controller并用新集合替换部分。

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; 
    }); 
}); 
}) 

5.Add方法projects_controller动态地改变集合的人。还有一行更新操作(用于更新零对象)。

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.Don't忘记路线:match "/projects/update_people/:id" => "projects#update_people"

0

如果您正在使用的Rails 3,那么你应该删除 “H(” 在application_helper。RB:

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

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