Ruby on rails 观测场误差

Ruby on rails 观测场误差,ruby-on-rails,Ruby On Rails,请注意,observe_字段似乎有点问题,我希望它能做的是,当我为country输入值时,该国家/地区下的状态显示出来。看起来您在嵌入Ruby时出错了:不插入任何内容,而插入函数的输出 试试这个: ``enter code here<%= form_for(@person) do |f| %> <% if @person.errors.any? %> <div id="error_explanation"> <h2><

请注意,observe_字段似乎有点问题,我希望它能做的是,当我为country输入值时,该国家/地区下的状态显示出来。看起来您在嵌入Ruby时出错了:不插入任何内容,而插入函数的输出

试试这个:

``enter code here<%= form_for(@person) do |f| %>
  <% if @person.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2>

      <ul>
      <% @person.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :country_id %><br />
    <%= f.collection_select :country_id, Country.all(:order => "name"), :id, :name, :prompt => "-- Select a Country --"%>
    <%= observe_field (:person_country_id, :url => {:action => "update_state_div"}, :with => 'person_country_id'%>
  </div>
  <div class="field">
    <%= f.label :state_id %><br />
    <%= f.collection_select :state_id, State.all(:order => "name"), :id, :name, :prompt => "-- Select a State --"%>


    <%observe_field 'person_country_id', :url => {:action => "update_state_div"}, :with => 'person_country_id'%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
进入控制台

这似乎与jQuery不兼容,因此您可能必须删除jQuery,或者编写自己的javascript来执行您想要的操作

还有一个编辑:


这个问题在普通JavaScript/jQuery和HTML中解决了这个问题:。您应该能够为Ruby on Rails修改它。

看起来您在嵌入Ruby时犯了一个错误:不插入任何内容,而插入函数的输出

试试这个:

``enter code here<%= form_for(@person) do |f| %>
  <% if @person.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2>

      <ul>
      <% @person.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :country_id %><br />
    <%= f.collection_select :country_id, Country.all(:order => "name"), :id, :name, :prompt => "-- Select a Country --"%>
    <%= observe_field (:person_country_id, :url => {:action => "update_state_div"}, :with => 'person_country_id'%>
  </div>
  <div class="field">
    <%= f.label :state_id %><br />
    <%= f.collection_select :state_id, State.all(:order => "name"), :id, :name, :prompt => "-- Select a State --"%>


    <%observe_field 'person_country_id', :url => {:action => "update_state_div"}, :with => 'person_country_id'%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
进入控制台

这似乎与jQuery不兼容,因此您可能必须删除jQuery,或者编写自己的javascript来执行您想要的操作

还有一个编辑:


这个问题在普通JavaScript/jQuery和HTML中解决了这个问题:。您应该能够为Ruby on Rails修改它。

将以下内容添加到您的GEM文件并运行捆绑包:

rails plugin install git://github.com/rails/prototype_legacy_helper

将以下内容添加到GEM文件并运行捆绑包:

rails plugin install git://github.com/rails/prototype_legacy_helper
啊。observe_字段不再包含在Rails中-请参阅my edit for info.Ahh。观察_字段不再包含在Rails中-有关信息,请参阅我的编辑。