jquery - 使用 Rails 4 和 JQuery 添加\删除嵌套表单中的项目

标签 jquery ruby-on-rails-4

我使用的是 ruby​​ 1.9.3 和rails 4.1。
我对 ruby​​ 很陌生,以前没有任何 JavaScript 知识。

我有几个简单的有很多关系和一个有很多通过关系。

class Service < ActiveRecord::Base
  has_many :websites,   :dependent => :destroy
  has_many :timetables, :dependent => :destroy
  has_many :service_places, :dependent => :destroy
  has_many :places, through: :service_places
end

class ServicePlace < ActiveRecord::Base
  belongs_to :service
  belongs_to :place
end

class Website < ActiveRecord::Base
  belongs_to :service
end

class Place < ActiveRecord::Base
  has_many :service_places    
  has_many :services, through: :service_places
end

class Timetable < ActiveRecord::Base
  belongs_to :service
end

我已经在服务表单(_form.html.erb)中为每个呈现良好的关联创建了嵌套表单,所有 CRUD 功能都可以工作。

<%= form_for(@service, :html => {:class => "form-horizontal", :role => "form"}) do |f| %>

  <%= render 'wcc_style/layouts/error_messages', object: @service %>

  <!-- I've skipped some of the html for the service details to just rendering the forms... -->

  <!-- Adds the Service_Places associations -->
  <%= f.fields_for :service_places do |service_places| %>
    <%= render "service_place_fields", :f => service_places %>
  <%end %>

  <!-- Adds the website associations via a partial -->
  <%= f.fields_for :websites do |website| %>
    <%= render "website_fields", :f => website %>
  <%end %>
  <%= link_to_add_fields "Add a website", f, :websites, "btn btn-default", "Add a new website" %>


  <!-- Adds the timetables associations via a partial -->
  <%=f.fields_for :timetables do |timetable| %>
    <%= render "timetable_fields", :f => timetable %>
  <%end %>

  <%= f.submit 'Save', :class => 'btn btn-primary' %>
  <%= link_to 'Cancel', services_path, :class => "btn btn-default", :role =>"button" %>

<% end %>

网站部分示例 (_website_fields.html.erb)

<!-- This partial holds the websites information for main service form -->
<div class="form-group"> 
  <%= f.label :website, :class=>'col-sm-2 control-label' %>
  <div class="col-sm-4">
    <%= f.text_field :url, :class => "form-control", :placeholder => "Service Provider Website" %>
  </div>
</div>

我现在想做的是添加功能,以便在添加\编辑服务记录时能够添加和删除与服务相关的项目(例如网站、时间表、service_places)。我看过很多与此相关的帖子,但没有看到使用 jquery 和 Rails 4 的良好工作解决方案。

对此进行了一些研究,我发现了旧的 Railcast 196 和 197。它们已经过时,并且在 Rails 4 中不起作用。更多研究表明命令已贬值。我看到有一个较新版本的railscasts,我还没有看过。我确实阅读了修订版的评论,它表明它使用了coffescript,尽管我无法确认这一点。

我还看到了使用 cocoon gem 和nested_form_fields 的其他潜在解决方案的链接。两者似乎都使用我不熟悉的 HAML。

我尝试使用 jquery 来完成此操作,以帮助我理解 javascript 流程(因为我需要在开发完成后支持开发,而我们的标准是尽可能使用 JQuery)。

我已经成功地获得了添加类型的工作,因为它在第一次按下时呈现网站标签和复选框,但随后的点击只是在消失之前再次呈现大约一秒钟。这个位置也不是我所期望的,在页面顶部,但我确信这取决于我们自己处理 CSS 的样式 gem 。我认为 javascript 不应该执行 POST,但它似乎是这样做的,因为它会在每次单击按钮时清除我的其他字段。

我的第一个问题是如何获得添加功能来添加新项目而不清除服务表单中的其他项目?

我的第二个问题是如何让删除功能发挥作用。我假设删除必须在部分表单生成器内(例如,每个项目删除一个)。

我在下面包含了应用程序帮助程序和 javascript(从各个帖子拼凑而成)。

application_helper.rb 模块应用程序助手

  #def link_to_remove_fields(name, f)
  #  f.hidden_field(:destroy) + link_to_function(name, "remove_fields(this)")
  #end

  # remove link doesn't work!!!!!!!!!
  def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

  # This now sort of works but the field disappears when it is created after about a second.  It also appears at the top of the page.  
  def link_to_add_fields(name, f, association, cssClass, title)  
    new_object = f.object.class.reflect_on_association(association).klass.new  
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|  
      render(association.to_s.singularize + "_fields", :f => builder)  
    end  
    link_to name, "#", :onclick => h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"), :class => cssClass, :title => title 
  end 

end

application.js

function remove_fields(link) {
    $(link).prev("input[type=hidden]").val("1");
    $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
    var new_id = new Date().getTime();
    var regexp = new RegExp("new_" + association, "g")
    $(link).parent().before(content.replace(regexp, new_id));
}

services_controller.rb

class ServicesController < ApplicationController
  # Set the service record before each action
  before_action :set_service, only: [:show, :edit, :update, :destroy]

  ################################################################################
  # Create
  ################################################################################
  def create
    @service = Service.new(service_params)

    respond_to do |format|
      if @service.save
        format.html { redirect_to @service, notice: 'Service was successfully created.' }
        format.json { render action: 'show', status: :created, location: @service }
      else
        @service.websites.build
        @service.timetables.build
        @service.service_places.build
        format.html { render action: 'new' }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end

  end


  ################################################################################
  # Delete
  ################################################################################
  def destroy
    @service.destroy

    respond_to do |format|
      format.html { redirect_to services_url , notice: 'Service was successfully deleted.' }
      format.json { head :no_content }
    end
  end


  ################################################################################
  # Edit
  ################################################################################
  def edit
    @service.websites.build
    @service.timetables.build
    @service.service_places.build
  end


  ################################################################################
  # Index
  ################################################################################
  def index
    @services = Service.order(:service_number).page params[:page]
  end


  ################################################################################
  # New
  ################################################################################
  def new
    @service = Service.new
    @service.websites.build
    @service.timetables.build
    @service.service_places.build
  end


  ################################################################################
  # Search
  ################################################################################
  def search
    @search_term = params[:search_term]
    if @search_term && @search_term.strip.empty?
      redirect_to root_path
    else
      @services = Service.search(@search_term).order(:service_number).page params[:page]
    end
  end


  ################################################################################
  # Show
  ################################################################################
  def show
  end


  ################################################################################
  # Update
  ################################################################################
  def update
    respond_to do |format|
      if @service.update(service_params)
        format.html { redirect_to @service, notice: 'Service was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end


  ################################################################################
  # Private methods
  ################################################################################
  private

    # Use callbacks to share common setup or constraints between actions.
    def set_service
      @service = Service.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def service_params
      params.require(:service).permit(:service_number, :operator, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday, :bank_holiday, :search_term, websites_attributes: [:id, :service_id, :url, :_destroy], timetables_attributes: [:id, :service_id, :url, :_destroy], service_places_attributes: [:id, :service_id, :position, :place_id, :_destroy],  places_attributes: [:id, :place_name, :active, :_destroy])
    end

end

在互联网上搜索后,我似乎找不到任何关于如何使用 jquery 和标准 ruby​​\rails 使其工作的链接。有谁知道任何有助于实现这一目标的链接或资源?

我并不反对使用 cocoon 或nested_form_fields 这样的 gem,假设我可以将 HAML 转换为 ERB,将 Coffeescript 转换为 javascript。作为 ruby​​\rails 和 javascript 的新手,让构建者做一些事情很棒,但是当出现问题时,我不知道如何修复它。

我感谢任何帮助或指导。

最佳答案

由于进展缓慢,我按照同事的建议使用了 cocoon。我按照这里的步骤操作:cocoon gem on github并设法让它发挥作用。我知道这并不能回答我的实际问题,但它确实为我提供了一种做我想做的事情的方法。我已经为其他人记录了这些步骤,以防他们觉得有用。我还将 HAML 转换为普通的 ruby​​,因为我对此更满意。

1) 将以下行添加到您的 gemfile 中:

gem "cocoon"

2) 将以下行添加到您的 app/assets/javascripts/application.js 文件中

//= require cocoon

3)修改我的服务表格(_form.html.erb)如下

<%= form_for(@service, :html => {:class => "form-horizontal", :role => "form"}) do |f| %>

  <%= render 'wcc_style/layouts/error_messages', object: @service %>

  <div class="form-group">
    <%= f.label :service_number, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-3">
      <%= f.text_field :service_number, :class => "form-control", :placeholder => "Service Number" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :operator, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-4">
      <%= f.text_field :operator, :class => "form-control", :placeholder => "Operator" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :monday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :monday, :class => "form-control, pull-left", :placeholder => "Monday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :tuesday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :tuesday, :class => "form-control, pull-left", :placeholder => "Tuesday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :wednesday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :wednesday, :class => "form-control, pull-left", :placeholder => "Wednesday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :thursday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :thursday, :class => "form-control, pull-left", :placeholder => "thursday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :friday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :friday, :class => "form-control, pull-left", :placeholder => "Friday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :saturday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :saturday, :class => "form-control, pull-left", :placeholder => "Saturday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :sunday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :sunday, :class => "form-control, pull-left", :placeholder => "Sunday" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :bank_holiday, :class=>'col-sm-2 control-label' %>
    <div class="col-sm-2">
      <%= f.check_box :bank_holiday, :class => "form-control, pull-left", :placeholder => "Bank Holiday" %>
    </div>
  </div>

<h3>Stops</h3>

  <!-- Adds the Service_Places associations via partial (sort applied in model) -->
  <div>
    <%= f.fields_for :service_places do |service_places| %>
      <%= render 'service_place_fields', :f => service_places %>
    <% end %>
    <div class="links">
      <%= link_to_add_association 'add service places', f, :service_places, :class => "btn btn-default" %>
    </div>
  </div>


<h3>Websites</h3>

  <!-- Adds the website associations via a partial -->
  <div>
    <%= f.fields_for :websites do |website| %>
      <%= render "website_fields", :f => website %>
    <%end %>
    <div class="links">
      <%= link_to_add_association 'add websites', f, :websites, :class => "btn btn-default" %>
    </div>
  </div>

<h3>Timetables</h3>

  <!-- Adds the timetables associations via a partial -->
  <div>
    <%=f.fields_for :timetables do |timetable| %>
      <%= render "timetable_fields", :f => timetable %>
    <%end %>
    <div class="links">
      <%= link_to_add_association 'add timetables', f, :timetables, :class => "btn btn-default" %>
    </div>
  </div>

  <br>

  <%= f.submit 'Save', :class => 'btn btn-primary' %>
  <%= link_to 'Cancel', services_path, :class => "btn btn-default", :role =>"button" %>

<% end %>

4) 将部分内容修改为如下所示。它需要 class="nested-fields"否则它将无法工作。

<div class="nested-fields">

  <%= f.label :website, :class=>'col-sm-2 control-label' %>

  <div class="col-sm-4">
    <%= f.text_field :url, :class => "form-control", :placeholder => "Service Provider Website" %>
  </div>

  <div>
    <%= link_to_remove_association "remove website", f, :class => "btn btn-default" %>
  </div>

</div>

我没有包含所有部分内容,但它们遵循相同的格式。

关于jquery - 使用 Rails 4 和 JQuery 添加\删除嵌套表单中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138798/

相关文章:

ruby-on-rails - 如何重构多个模型中出现的方法?

ruby-on-rails - 连接到 Vagrant 上的主机 MySQL 服务器

javascript - 未捕获的 TypeError : $(. ..).velocity 不是一个函数

JQuery 添加和删除斜杠

ruby-on-rails - 通过保持连接打开来加速 OpenUri?

ruby-on-rails - 在 Ruby on Rails 中格式化全名

javascript仅在刷新页面后加载

javascript - jquery 自动完成正在呈现未过滤的数据

jQuery AJAX 分页如何防止指数循环?

jquery - 扩展一个元素来填充 li