问题描述
我正在用Rails 4.0开发一个应用程序,但是我遇到了一个问题,即turbolinks无法与我拥有的某些jQuery代码配合使用.我有一个相关的QuoteItems模型的Quote模型.我正在使用accepts_nested_attributes_for和一些jQuery填充订单项表单.
I am developing an application in Rails 4.0 and I'm having an issue with turbolinks not playing nice with some jQuery code I have. I have a Quote model that has a related QuoteItems model. I am using accepts_nested_attributes_for and some jQuery to populate the line items form.
当我单击将我带到new_quote_path的链接时,动态链接不会触发javascript代码.当我刷新页面时,该表格非常有效.我喜欢Turbolink,因为它超级快,但不确定如何在开发中使用它.这是一些代码.
When I click on a link bringing me to the new_quote_path, The dynamic link doesn't fire the javascript code. When I refresh the page, the form WORKS GREAT. I like turbolinks as it is super fast, but not sure how to get this to work in development. Here's some code.
在quotes.js.coffee中
in quotes.js.coffee
jQuery ->
$('form').on 'click', '.remove_line_items', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$('form').on 'click', '.add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
报价查看new.html.erb
Quotes view new.html.erb
<%= form_for @quote, :class => "hello" do |f| %>
<fieldset>
<p>
<%= f.label :quote_date, "Date of Quote" %> <br/>
<%= f.text_field :quote_date %>
</p>
<p>
<%= f.label :good_through %> <br/>
<%= f.text_field :good_through %>
</p>
<p>
<%= f.label :quote_number %><br/>
<%= f.text_field :quote_number %>
</p>
<p>
<%= f.label :customer_id, "Customer" %><br/>
<%= select(:quote, :customer_id, Customer.all.collect {|c| [ c.fname, c.id ] }, :prompt => "Select Customer") %>
</p>
<%= f.fields_for :quote_items do |builder| %>
<%= render 'quote_item_fields', :f => builder %>
<% end %>
<%= link_to_add_fields "Add Line Item", f, :quote_items %>
<p>
<%= f.submit %>
</p>
</fieldset>
<% end %>
推荐答案
我遇到了同样的问题,如果您想保持Turbolink正常工作并启用动态jquery,请尝试以下gem: jquery-turbolinks
I was having the same problem, if you want to keep turbolinks working and your dynamic jquery on, try this gem: jquery-turbolinks
希望有帮助
这篇关于Rails 4 TurboLinks和jQuery Dynamic Links表现不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!