简体   繁体   English

jQuery在Rails 3.2.8中不起作用

[英]Jquery is not working in Rails 3.2.8

my name is Pamela and i'm new to this version of rails, i'm trying to put some validations and datepickers in a form, but for some reason the jquery is not working, i've searched a lot and tried several things but they didn't work. 我的名字叫Pamela,我是这个版本的Rails的新手,我正在尝试以表单的形式添加一些验证和日期选择器,但是由于某种原因,jQuery无法正常工作,我进行了很多搜索并尝试了一些操作,但是他们没有工作。 Here's my application.js file: 这是我的application.js文件:

//= require jquery
//= require jquery_ujs
//= require jquery-ui-1.8.24.custom.min
//= require jquery.ui.datepicker-es
//= require jquery.validate
//= require messages_es

This is my application.html.erb: 这是我的application.html.erb:

<%= javascript_include_tag "application" %>

I ran: rails generate jquery:install and this is the result: 我跑了:rails生成jquery:install,这是结果:

deprecated You are using Rails 3.1 with the asset pipeline enabled, so this generator is not needed. 不推荐使用启用资产管道的Rails 3.1,因此不需要此生成器。 The necessary files are already in your asset pipeline. 必要的文件已经在您的资产管道中。 Just add //= require jquery and //= require jquery_ujs to your app/assets/javascripts/application.js If you upgraded your app from Rails 3.0 and still have jquery.js, rails.js, or jquery_ujs.js in your javascripts, be sure to remove them. 只需添加//= require jquery//= require jquery_ujs到您的应用程序/资产//= require jquery_ujs如果您从Rails 3.0升级了应用程序,但在JavaScripts中仍然包含jquery.js,rails.js或jquery_ujs.js ,请务必将其删除。 If you do not want the asset pipeline enabled, you may turn it off in application.rb and re-run this generator. 如果您不希望启用资产管道,则可以在application.rb中将其关闭,然后重新运行此生成器。

Can anyone help? 有人可以帮忙吗?

Thank you. 谢谢。

Updated: 更新:

This is my form: 这是我的表格:

<%= form_for(@contrato) do |f| %>
  <% if @contrato.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@contrato.errors.count, "error") %> no han permitido que el contrato sea guardado:</h2>
      <ul>
      <% @contrato.errors.full_messages.each do |msg| %>
        <li><font color="red"><%= msg %></font></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label 'Obra' %><br />
    <%= f.text_field :contrato_nombre_obra %>
  </div>
 <div class="field">
   <%= f.label 'Nombre Contratista/Empresa' %><br />
   <%= f.select :contratista_id, Contratista.find(:all, :order => "empresa").collect{|c| [c.empresa, c.id] } %>
  </div> 
  <div class="field">
    <%= f.label 'Municipio' %><br />
    <%#= f.number_field :tipo_norma_legal_id %>
    <%= f.select :municipio_id, Municipio.find(:all, :order => "nombre").collect{|c| [c.nombre, c.id] } %>
  </div>
    <div class="field">
    <%= f.label 'Localidad' %><br />
    <%#= f.number_field :tipo_norma_legal_id %>
    <%= f.select :localidad_id, Localidad.find(:all, :order => "nombre").collect{|c| [c.nombre, c.id] } %>
  </div>
  <div class="field">
    <%= f.label 'Tipo de Norma Legal' %><br />
    <%#= f.number_field :tipo_norma_legal_id %>
    <%= f.select :tipo_norma_legal_id, TipoNormaLegal.find(:all, :order => "tipo_norma_legal_descripcion").collect{|c| [c.tipo_norma_legal_descripcion, c.id] } %>
  </div>
  <div class="field">
    <%= f.label 'Nº Norma Legal '%><br />
    <%= f.text_field :contrato_nro_norma_legal %>
  </div>
  <div class="field">
    <%= f.label 'Año Norma Legal' %><br />
    <%= f.text_field :contrato_anio_norma_legal %>
  </div>
  <div class="field">
    <%= f.label 'Nº de Expediente (solo numeros)' %><br />
    <%= f.text_field :contrato_expte %>
  </div>
  <div class="field">
    <%= f.label 'Fecha del Contrato' %><br />
    <%= f.text_field :contrato_fecha %>
  </div>
  <div class="field">
    <%= f.label 'Monto de Contrato ($)' %><br />
    <%= f.text_field :contrato_monto %>
  </div>
  <div class="field">
    <%= f.label 'Plazo de Obra (dias)' %><br />
    <%= f.text_field :contrato_plazo_obra %>
  </div>
  <div class="field">
    <%= f.label 'Contrato Finalizado' %>
    <%= f.check_box :finalizado %>
  </div>
  <div class="actions">
    <br class="clearfix" />
    <%= f.submit 'Guardar', :class=>'button' %>
  </div>

<% end %>


<%= render(:partial => "javascripts") %>

This is the partial: 这是部分的:

<script type="text/javascript">
 $(document).ready(function () {
     $("#new_contrato").validate({

        rules: {
        "contrato[contrato_nombre_obra]": {required: true, minlength: 3 , maxlength: 60 },
        "contrato[contrato_monto]": {digits: true},
        "contrato[contrato_plazo_obra]": {required: true, digits: true}
        "contrato[contrato_contrato_expte]": {digits: true}
        }
      });
    // Datepicker
        $('#contrato_contrato_fecha').datepicker({
          changeMonth: true,
          changeYear: true,
        });


});
</script>

You should already have access to jQuery and Unobtrusive jQuery if you have generated the application and haven't changed the gemfile. 如果已生成应用程序且未更改gemfile,则应该已经可以访问jQuery和Unobtrusive jQuery。 Check if it includes jquery-rails. 检查它是否包含jquery-rails。 If it does, then you already have jquery ready to use. 如果是这样,则您已经可以使用jquery。 You don't need to run rails generate jquery:install . 您不需要run rails generate jquery:install

Well, first of all, thank you very much for helping me... i've tried another one last thing... i started from fresh, i've realized that maybe i deleted something in my effort to make javascript work, so i decided to created a the project again (not the ideal thing to do, right?), so i just copied my views in the new project, made the migrations, etc... And now at least the validations are working, i didn't try datepickers or other stuff yet (hopefully they'll work too). 好吧,首先,非常感谢您对我的帮助...我已经尝试了另一件事...我从头开始,我意识到也许我在努力使javascript工作时删除了一些东西,所以我决定再次创建一个项目(不是理想的事情,对吧?),所以我只是在新项目中复制了我的观点,进行了移植等。现在至少验证工作了,我没有还不要尝试使用日期选择器或其他工具(希望它们也可以工作)。 If ever know what happened, i'll let you know it may help someone else. 如果知道发生了什么,我会通知您,它可能会对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM