我正在使用ruby 1.9.3和rails 4.1。
我对ruby非常陌生,没有以前的javascripting知识。使用Rails 4和JQuery添加删除嵌套窗体中的项目
我有一对简单的有很多关系,一个有很多关系。
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的良好工作解决方案。
在对此做了一些研究之后,我发现了旧的轨道交通196和197.这些都过时了,并且didn在铁轨4工作。更多的研究表明贬值的命令。我看到有一个更新版本的railscasts,我还没有看过。我确实阅读了修订版的评论,并表明它使用了coffeescript,尽管我无法证实这一点。
我也看到了使用cocoon gem和nested_form_fields的其他潜在解决方案的链接。两者似乎都使用我不熟悉的HAML。我试图用jQuery来做到这一点,以帮助我更好地理解javascript过程(因为我需要在完成时支持开发,我们的标准是尽可能使用JQuery)。
我已经设法得到添加类的工作,它在第一次按下时呈现网站标签和复选框,但随后的点击只是在消失之前再次呈现此消息约一秒。这个位置并不是我期望它在页面顶部的位置,但我相信这是我们自己的与CSS有关的造型宝石。我认为JavaScript不应该做一个POST,这看起来是因为它清除了每个按钮点击我的其他领域。
我的第一个问题是,如何在不清除服务表单中的其他项的情况下添加新项目?
我的第二个问题是如何获得删除功能的工作。我假设删除必须位于部分表单生成器中(例如每个项目删除一个)。
我已经在下面包含了应用程序助手和javascript(从各种帖子拼凑在一起)。
application_helper.rb 模块ApplicationHelper
#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和标准红宝石\轨工作的任何链接。有没有人知道任何有助于实现这一目标的链接或资源?
我不反对使用宝石像茧或nested_form_fields,假设我可以将HAML转换为ERB和咖啡脚本为JavaScript。作为ruby \ rails和JavaScript的新手,有建设者在做东西是很好的,但当它出错时,我不知道如何解决它。
我感谢任何帮助或指导。