紅寶石2.0.0至控制器,導軌4.0.3傳遞實例變量從視圖通過JavaScript
時選擇年被觸發時,JavaScript的當前從視圖到經過實例變量(@car)的名稱控制器作爲字符串。我需要傳遞一個對實例變量的引用,而不僅僅是字符串名稱。這可能嗎?我會很感激幫助。
編輯:我已經嘗試更換汽車的引用如下,但我收到語法錯誤。
var car = $("#<%= @car %>");
Error: Syntax error, unrecognized expression: #<%= @car %>
的觀點是:
<div class="span8">
<%= simple_form_for [:admin, @car],
defaults: {label: false},
html: {id: 'new_admin_car', class: 'form-vertical'},
wrapper: :vertical_form,
wrapper_mappings: {
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean
} do |f| %>
<%= f.input(:stock_number, {input_html: {form: 'new_admin_car', car: @car}, autocomplete: :off, placeholder: 'Stock number?'}) %>
<%= f.input(:ymm_year_id, {input_html: {form: 'new_admin_car', car: @car}, collection: YmmYear.all.order("year desc").collect{|c| [c.year, c.id]}, prompt: "Year?"}) %>
<%= render partial: "makes", locals: {form: 'new_admin_car', car: @car} %>
<%= render partial: "models", locals: {form: 'new_admin_car', car: @car} %>
<input type="submit" form="new_admin_car" value="Create Car" class="btn btn-default btn btn-primary">
<% end %>
</div>
當前的腳本是:
// when the #year field changes
$("#car_ymm_year_id").change(function() {
// make a GET call and replace the content
// First select identifies what has been selected, or fired
var year = $('select#car_ymm_year_id :selected').val();
// Pull the variables from the input_html tag
var form = $('select#car_ymm_year_id').attr("form");
var car = $('select#car_ymm_year_id').attr("car");
// Routes to the controller action
$.post('/admin/cars/make_list/',
{
form: form,
year: year,
car: car
},
function (data) {
$("#car_ymm_make_id").html(data);
});
return false;
});
你的意思是從控制器傳遞var來查看?或者是你在JS中進行GET調用的地方? – Aryess 2014-08-28 09:41:36
@car通過常規方法來自控制器的視圖。當年份發生變化時,JS會觸發並將選定年份傳回給控制器,以便它可以構建該年份的製造清單。當火災發生時,我需要通過JavaScript從視圖傳遞迴控制器的汽車實例。 – 2014-08-28 09:48:22
@Aessess我認爲我的評論是錯誤的。往上看。 – 2014-08-28 10:04:58