Rails country-state_select gem javascript 不工作
Rails country-state_select gem javascript not working
安装 country_state_select gem 后,它成功加载了国家,但我选择的第一个国家加载了相应的州,但在选择另一个国家后,其相应的州不会加载,而是属于的州我选择的最后一个国家。我曾多次尝试关闭服务器以重置它,但仍然会这样做。
<%= simple_form_for(@office_item) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if
f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.file_field :pictures, multiple:true %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.association :category, label_method: :title, value_method: :id,
include_blank: false, prompt: "Select Category" %>
<%= f.input :address %>
<%= f.input :country, collection: CountryStateSelect.countries_collection
%>
<%= f.input :state, CountryStateSelect.state_options(label: "State /
Province", form: f, field_names: { :country => :country, :state =>
:state } ) %>
<%= f.input :city, CountryStateSelect.city_options(label: "City ",
form: f, field_names: { :state => :state, :city => :city } ) %>
<%= f.input :pricing, label: "Price" %>
<%= f.input :terms, label: "Terms and Conditions" %>
<%= f.input :is_active, label: "Publish" %>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-secondary" %>
</div>
<% end %>
<script>
$(document).on('ready page:load', function() {
return CountryStateSelect({
country_id: "country",
state_id: "state"
city_id: "city"
});
});
</script>
我的期望是每个选择的国家将分别加载其对应的州和城市
此 gem 文档有点过时,因此当您使用 Rails 5 时,第一个 script
行应该是
$(document).on('turbolinks:load', function() {
或者干脆
$(function() {
然后重新加载页面并查看浏览器的控制台。如果您看到 CountryStateSelect is not defined
错误,您应该将 gem 的 javascript 资产添加到您的 application.js
文件
//= require country_state_select
您还应确保使用 right ids 个元素 <select>
个
此 gem 使用 POST JSON 更改 <select>
值的请求,因此如果您使用 Can't verify CSRF token authenticity.
您可以通过添加到 ApplicationController
class
来禁用此验证
class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
end
安装 country_state_select gem 后,它成功加载了国家,但我选择的第一个国家加载了相应的州,但在选择另一个国家后,其相应的州不会加载,而是属于的州我选择的最后一个国家。我曾多次尝试关闭服务器以重置它,但仍然会这样做。
<%= simple_form_for(@office_item) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if
f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.file_field :pictures, multiple:true %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.association :category, label_method: :title, value_method: :id,
include_blank: false, prompt: "Select Category" %>
<%= f.input :address %>
<%= f.input :country, collection: CountryStateSelect.countries_collection
%>
<%= f.input :state, CountryStateSelect.state_options(label: "State /
Province", form: f, field_names: { :country => :country, :state =>
:state } ) %>
<%= f.input :city, CountryStateSelect.city_options(label: "City ",
form: f, field_names: { :state => :state, :city => :city } ) %>
<%= f.input :pricing, label: "Price" %>
<%= f.input :terms, label: "Terms and Conditions" %>
<%= f.input :is_active, label: "Publish" %>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-secondary" %>
</div>
<% end %>
<script>
$(document).on('ready page:load', function() {
return CountryStateSelect({
country_id: "country",
state_id: "state"
city_id: "city"
});
});
</script>
我的期望是每个选择的国家将分别加载其对应的州和城市
此 gem 文档有点过时,因此当您使用 Rails 5 时,第一个
script
行应该是$(document).on('turbolinks:load', function() {
或者干脆
$(function() {
然后重新加载页面并查看浏览器的控制台。如果您看到
CountryStateSelect is not defined
错误,您应该将 gem 的 javascript 资产添加到您的application.js
文件//= require country_state_select
您还应确保使用 right ids 个元素
<select>
个此 gem 使用 POST JSON 更改
来禁用此验证<select>
值的请求,因此如果您使用Can't verify CSRF token authenticity.
您可以通过添加到ApplicationController
classclass ApplicationController < ActionController::Base protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' } end