[英]Rails4: Form-Update via Ajax not working -> Template error
I am trying to update a section in a form when the user enters a valid date. 当用户输入有效日期时,我正在尝试更新表单中的部分。 CoffeeScript is initiating a call to the controller and the controller starts a '.js.erb' action for rendering a form partial. CoffeeScript正在启动对控制器的调用,并且控制器启动“ .js.erb”操作以呈现部分表单。 What I want is to supply the user-entered date to a 'autocomplete' field, so only values with a certain validity date will be returned and offered... 我想要的是将用户输入的日期提供给“自动完成”字段,因此将仅返回并提供具有特定有效日期的值...
See code below: 参见下面的代码:
flights.s.coffee: flight.s.coffee:
$ ->
$("#flight_f_date").focusout ->
$.ajax
url: "/flight/populateAircraftsSelection"
data:
f_date: $('#flight_f_date').val()
dataType: "script"
My controller function looks like: 我的控制器功能如下:
def populateAircraftsSelection
puts "AircraftsSelection" + params[:f_date.to_s]
@flight_date = params[:f_date].to_s
respond_to do |format|
format.js
end
end
populateAircraftsSelection.js.erb: populateAircraftsSelection.js.erb:
$("#flight_test").html("<%= escape_javascript render partial: 'test', :locals => { flight: @flight, flight_date: @flight_date } %>");
and the _test.htnl.erb partial 和_test.htnl.erb部分
<div id="#test">
<%= f.label :aircraft_id, "Kennzeichen" %><br>
<%= f.hidden_field :aircraft_id %>
<%= f.autocomplete_field :helper_registration_field ,
autocomplete_aircraft_registration_flights_path(:flight_date =>
@flight_date.to_s),
:update_elements => { :id => '#flight_aircraft_id', :acfttype =>
'#flight_helper_aircraft_field' } %>
</div>
I am getting the following error: 我收到以下错误:
ActionView::Template::Error (undefined local variable or method `f' for #<#
<Class:0x007fee2cb62c28>:0x007fee298a0ec0>):
1: <div id="#test">
2:
3:
4: <%= f.label :aircraft_id, "Kennzeichen" %><br>
5: <%= f.hidden_field :aircraft_id %>
6:
7: <%= f.autocomplete_field :helper_registration_field ,
autocomplete_aircraft_registration_flights_path(:flight_date => @flight_date.to_s),
app/views/flights/_test.html.erb:4:in
`_app_views_flights__test_html_erb__1386346884294841760_70330389540980'
app/views/flights/populateAircraftsSelection.js.erb:1:in
`_app_views_flights_populate_ircrafts_election_js_erb__2604210186479166916_70330440189180'
app/controllers/flights_controller.rb:179:in `populateAircraftsSelection'
I thought 'f' must be know, because I am adding this partial into a bigger form where 'f' is already known..... 我以为必须知道'f',因为我将此部分添加到一个更大的形式中,其中已经知道'f'.....
What I am doing wrong? 我做错了什么? Or: is there a much better way to update aa clue inside a form after user interaction.....? 或者:在用户交互之后是否有更好的方法来更新表单内的提示?
Thanks in advance for any help you can provide... 预先感谢您提供的任何帮助...
Dirk 短剑
OK, found a trick on my own - using a session variable. 好的,我自己找到了一个窍门-使用会话变量。 I know, it's dirty, and has nothing to do with AJAX as it was designed, - but it works... Only problem to distinguish in two or three location when to use params and when to use session. 我知道,它很脏,并且与AJAX的设计无关,但是它可以工作...区分两个或三个位置,何时使用参数和何时使用会话的唯一问题。 But still using AJAX and Coffeescript to initiate this kind of a call.... 但是仍然使用AJAX和Coffeescript发起这种调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.