使用Ajax在rails

时间:2015-06-02 17:33:12

标签: ruby-on-rails ajax

这篇文章是解决为什么ajax调用在我的项目中不起作用的最后机会。希望你们能帮助我。

我唯一想要的是提供部分"命令"单击" link_to"到我的管理页面按钮。

这就是我在控制器中所称的" masters":

class MastersController < ApplicationController
  before_action :logged_in_master, only: [:index, :edit, :update, :destroy]
  before_action :correct_master,   only: [:edit, :update]


  def administration
  @orders = Order.all       
    respond_to do |format|
    format.html
    format.json
    format.js
    end
  end

我的观点称为&#34; administration.html.erb&#34;:

<% provide(:title, "Administration") %>
<h1>Manage your site</h1>


<ul class="nav nav-pills">
  <li role="presentation" class="active"><%= link_to "Masters", masters_path %></li>
  <li role="presentation"><%= link_to "Orders", orders_path, action:remote => true %></li>
  <li role="presentation"><%= link_to "Settings", edit_master_path(current_master) %></li>
  <li role="presentation"><%= link_to "Services", manage_path %></li>
  <li style="float:right;" role="presentation"><%= link_to "Log out", logout_path, method: "delete" %></li>
</ul>


<div class="activity">

</div>

我的js格式的视图名为&#34; administration.js.erb&#34;:

var orders_inject = $("<%= escape_javascript(render (:partial => 'orders')) %>");
$('.activity').html(orders_inject); 

and my partial itself called "orders":

<table class="table table-hover table-striped">
  <thead>
    <tr>
      <th>Name</th>
      <th>Field</th>
      <th>Description</th>
      <th>Address</th>
      <th>Time</th>
      <th>Mobile number</th>
      <th>Photo</th>
      <th>Master</th>
    </tr>
  </thead>

  <tbody>
    <% @orders.each do |order| %>
      <tr class="active">
        <td><%= order.name %></td>
        <td><%= order.field %></td>
        <td><%= order.description %></td>
        <td><%= order.address %></td>
        <td><%= order.datetime %></td>
        <td><%= order.mobile_number %></td>
        <td><%= order.photo %></td>   
        <td><%= order.master_id %></td> 
        <td><%= link_to 'Add master', edit_order_path(order), class: 'btn btn-info btn-sm' %></td>   
        <td><%= link_to 'Delete order', order, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-xs' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

所以,请帮助我弄清楚,因为我已经阅读了几乎一周的各种相关帖子,但仍然没有解决这个错误。真的很感激任何评论和建议。提前谢谢!

1 个答案:

答案 0 :(得分:0)

$('.activity').html("<%= j raw render 'orders' %>")

在哪里&#39;订单&#39;位于app / views / masters / orders.html.erb