我正在关注动态下拉菜单Rail Cast tutorial。但它没有提供预期的输出。我如何知道js.erb文件中的方法是否正在运行? 这是我的控制器
class JavascriptsController < ApplicationController
def dynamic_states
@countries= Country.all
@venues= Venue.all
end
end
我尝试放下拉菜单的地方。
<p>
<label for="event_country_id">Country:</label>
<%= f.collection_select :country_id, @countries, :id, :name, :prompt => "Select a Country" %>
</p>
<p id="state_field">
<label for="event_venue_id">State or Province:</label>
<%= f.collection_select :venue_id, @venues, :id, :place, :prompt => "Select a State" %>
</p>
这是我的架构
create_table "countries", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "events", :force => true do |t|
t.string "name"
t.date "start_at"
t.date "end_at"
t.datetime "created_at"
t.datetime "updated_at"
t.string "trainer_id"
t.string "venue_id"
t.string "description"
t.boolean "holy"
t.integer "nxt"
t.string "country_id"
end
create_table "trainers", :force => true do |t|
t.string "name"
t.string "email"
t.integer "contact_num"
t.datetime "created_at"
t.datetime "updated_at"
t.string "color"
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password"
t.string "salt"
t.string "remember_token"
t.boolean "admin"
end
create_table "venues", :force => true do |t|
t.string "country_id"
t.string "place"
t.string "color"
t.datetime "created_at"
t.datetime "updated_at"
end
这是我的dynamic_states.js.erb
var venues = new Array();
<% for venue in @venues %>
venues.push(new Array(<%= venue.country_id%>, '<%=h venue.place%>', <%= venue.id %>));
<% end %>
function countrySelected() {
alert("test")
country_id = $('event_country_id').getValue();
options = $('event_venue_id').options;
options.length = 1;
venues.each(function(venue) {
if (venue[0] == country_id) {
options[options.length] = new Option(venue[1], venue[2]);
}
});
if (options.length == 1) {
$('state_field').hide();
} else
{
$('state_field').show();
}
}
document.observe('dom:loaded', function() {
countrySelected();
$('event_country_id').observe('change', countrySelected);
});
请有人解释为什么不工作?