【问题标题】:Handling js.erb files in Rails 3在 Rails 3 中处理 js.erb 文件
【发布时间】:2011-12-13 22:01:55
【问题描述】:

我正在转换一个标准的脚手架生成的应用程序以使用 AJAX 和 JQuery,其方式与 http://stjhimy.com/posts/07-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript 中指定的方式类似。

我遵循了所有的指示:

  • 使用 2 个部分创建复合索引视图;
  • 更新了Controller,只保留了Index、Create、Edit、Update和Destroy动作;
  • 为使用 JQuery 函数更新 DOM 的创建、编辑、更新和销毁操作创建了 js.erb 文件。

似乎根本无法访问 js.erb 文件。将 js.erb 与视图文件一起放入,例如app/views/customers/create.js.erb

create.js.erb 的代码是:

<% if @customer.errors.any? -%> 

   /*Hide the Flash Notice div*/
   $("#flash_notice").hide(300);

   /*Update the html of the div customer_errors with the new one*/
   $("#customer_errors").html("<% @customer.errors.full_message.each do |msg| %>
                              <li><%= msg %></li>
                              <% end %>");

   /*Show the div customer_errors*/
   $("#cust0mer_errors").show(300);

<% else -%>

   /*Hide the div customer_errors*/
   $("#customer_errors").hide(300);

   /*Update the html of the div flash_notice with the new one */
   $("#flash_notice").html("<%= flash[:notice] %>");

   /*Show the flash_notice div*/
   $("#flash_notice").show(300);

   /*Clear the entire form*/
   $(":input:not(input[type=submit])").val("");

   /*Replace the html of the div posts_list with the updated new one*/
   $("#customers_list").html("<%= render "customers" %>";

   <% end %>

表格代码如下:

index.html.erb 文件

<div id="customer_form"><%= render 'form' %></div>
<div id="customers_list"><%= render 'customers' %></div>

_form.html,erb部分文件

<%= form_for(@customer, :remote => true) do |f| %>


  <div id="customer_errors" style="display:none"></div>

  <div class="field">
    <%= f.label :firstname %>
    <%= f.text_field :firstname %>
    <%= f.label :lastname %>
    <%= f.text_field :lastname %>
  </div>
  <div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
    <%= f.label :phone %>
    <%= f.text_field :phone %>
  </div>
  <div class="field">
    <%= f.label :password %>
    <%= f.text_field :password %>
    <%= f.label :address_id %>
    <%= f.text_field :address_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

_customers.html.erb 部分的文件是:

<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Password</th>
    <th>Address</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @customers.each do |customer| %>
  <tr>
    <td><%= customer.firstname %></td>
    <td><%= customer.lastname %></td>
    <td><%= customer.email %></td>
    <td><%= customer.phone %></td>
    <td><%= customer.password %></td>
    <td><%= customer.address_id %></td>
    <td><%= link_to 'Edit', edit_customer_path(customer), :remote => true %></td>
    <td><%= link_to 'Destroy', customer, :remote => true, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

编辑

最新版本的客户控制器:

class CustomersController < ApplicationController

  before_filter :load

  def load
    @customers = Customer.all
    @customer  = Customer.new
  end

  def index   
  end


  def create
    @customer = Customer.new(params[:customer])
    if @customer.save
      flash[:notice] = "Customer was successfully created."
      @customers = Customer.all
      respond_to do |format|
        format.js
      end 

    end
  end 

  def edit
    @customer = Customer.find(params[:id])
    respond_to do |format|
      format.js
    end
  end

  def update
    @customer = Customer.find(params[:id])
    if @customer.update_attributes(params[:customer])
      flash[:notice] = "Customer was successfully updated."
      @customers = Customer.all
      respond_to do |format|
        format.js
      end   
    end
  end  

  def destroy
    @customer = Customer.find(params[:id])
    @customer.destroy
    flash[:notice] = "Customer successfully destroyed"
    @customers = Customer.all
    respond_to do |format|
      format.js
    end
  end
end

我将jquery.js填充到rails.js后,最新版的布局模板包含如下标签

    <%= stylesheet_link_tag :all %>
      <%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' %>
      <%= javascript_include_tag 'rails' %>
      <%= csrf_meta_tag %>

最新日志:

Started GET "/customers" for 127.0.0.1 at Wed Dec 14 21:16:14 +0000 2011
  Processing by CustomersController#index as HTML
  Customer Load (1.3ms)  SELECT "customers".* FROM "customers"
Rendered customers/_form.html.erb (14.1ms)
Rendered customers/_customers.html.erb (28.1ms)
Rendered customers/index.html.erb within layouts/application (47.6ms)
Completed 200 OK in 74ms (Views: 56.3ms | ActiveRecord: 1.3ms)


Started GET "/customers/13/edit" for 127.0.0.1 at Wed Dec 14 21:17:20 +0000 2011
  Processing by CustomersController#edit as JS
  Parameters: {"id"=>"13"}
  Customer Load (1.1ms)  SELECT "customers".* FROM "customers"
  Customer Load (0.5ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" = 13 LIMIT 1
Rendered customers/_form.html.erb (16.1ms)
Rendered customers/edit.js.erb (17.6ms)
Completed 200 OK in 43ms (Views: 27.6ms | ActiveRecord: 1.5ms)


Started GET "/customers/13/edit" for 127.0.0.1 at Wed Dec 14 21:17:31 +0000 2011
  Processing by CustomersController#edit as JS
  Parameters: {"id"=>"13"}
  Customer Load (1.0ms)  SELECT "customers".* FROM "customers"
  Customer Load (0.3ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" = 13 LIMIT 1
Rendered customers/_form.html.erb (25.9ms)
Rendered customers/edit.js.erb (28.8ms)
Completed 200 OK in 52ms (Views: 39.0ms | ActiveRecord: 1.3ms)


Started DELETE "/customers/18" for 127.0.0.1 at Wed Dec 14 21:18:31 +0000 2011
  Processing by CustomersController#destroy as JS
  Parameters: {"id"=>"18"}
  Customer Load (1.0ms)  SELECT "customers".* FROM "customers"
  Customer Load (0.4ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" = 18 LIMIT 1
  AREL (0.4ms)  DELETE FROM "customers" WHERE "customers"."id" = 18
  Customer Load (0.7ms)  SELECT "customers".* FROM "customers"
Rendered customers/_customers.html.erb (120.3ms)
Rendered customers/destroy.js.erb (122.1ms)
Completed 200 OK in 198ms (Views: 134.1ms | ActiveRecord: 2.5ms)


Started GET "/customers" for 127.0.0.1 at Wed Dec 14 21:20:00 +0000 2011
  Processing by CustomersController#index as HTML
  Customer Load (1.6ms)  SELECT "customers".* FROM "customers"
Rendered customers/_form.html.erb (19.1ms)
Rendered customers/_customers.html.erb (23.8ms)
Rendered customers/index.html.erb within layouts/application (50.6ms)
Completed 200 OK in 76ms (Views: 54.9ms | ActiveRecord: 1.6m

【问题讨论】:

    标签: javascript jquery ajax ruby-on-rails-3


    【解决方案1】:

    当你创建你的formlink_to 对象时,你需要确保它们上面有一个:remote =&gt; true,否则路由不会通过JS 渲染动作。相反,它将使用默认 HTML 呈现它。

    一个例子:

    <%= form_for(@post, :remote => true) do |f| %>
    

    <%= link_to "Edit", edit_post_path(post), :remote => true %>
    

    另外,请确保您安装了最新的 jQuery 和 jQuery Rails 适配器:https://github.com/rails/jquery-ujs

    附带说明一下,如果您为 CRUD 操作执行 100% ajax,则在上面的代码中不需要 format.html,因为您将渲染的只是 JS 文件 (format.js)。


    更新: 我认为您误解了一些事情...您正在阅读的教程仅讨论将 CRUD(创建、读取、更新、删除)操作更改为 100% ajax 调用,这意味着它们是唯一会响应渲染@ 987654331@ 文件。因此,当您检查页面是否为Processing SomeController#some_action as JS 时,它将应用于您的客户控制器中的createshowupdatedestroy 操作。

    要在 Rails 3.0 上安装 jQuery 和 jQuery-UJS,请遵循以下说明:

    如果存在 rails.js 文件,请务必删除它,而是使用复制到公共目录的新 jquery_ujs.js 文件。如果出现提示,请选择覆盖 jquery_ujs.js。

    然后运行rails generate jquery:install

    将您的布局模板更改为:

    <%= stylesheet_link_tag :all %>
    <%= javascript_include_tag :defaults %>
    <%= csrf_meta_tag %>
    

    我不得不说,你正在阅读的教程是关于我读过的最糟糕的教程。由于您似乎对 Ruby on Rails 很陌生,所以我强烈建议阅读不同的教程(如果您仍想阅读有关 AJAX w/Rails 的内容,请阅读这篇文章:http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ 甚至是真正的喜欢这个可以更好地学习 Rails 本身:http://ruby.railstutorial.org/ruby-on-rails-tutorial-book)。

    【讨论】:

    • 我已经把表格放好了 - 你还想要什么吗?
    • 您可以在提交表单帖子或单击链接时发布服务器日志吗?另外,检查浏览器的控制台,看看是否有任何 JS 错误。
    • 把我能做的放在那里——还用 create.js.erb 文件修正了一些错别字。很高兴知道一个人并不孤单。
    • 您的 create 控制器操作中仍有错误:@customers = customer.all 应该是 @customers = Customer.all... 而且,您的 create.js.erb 文件中的另一个错误:$("#cust0mer_errors").show(300); 应该是 @ 987654344@ 您能否验证您是否安装了 jQuery 和 jQuery Rails 适配器 (github.com/rails/jquery-ujs) 并删除了 format.html 引用?
    • 我一直询问 jQuery 的原因是因为您的 Rails 服务器日志显示 Processing by CustomersController#create as HTML (format.html) 但如果 JS 调用正常工作(通过 jQuery),那么它应该是 @987654347 @ (format.js) 代替
    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2015-01-23
    • 2023-03-11
    • 2011-08-24
    相关资源
    最近更新 更多