You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 4.2中用jQuery删表格行遇ActionView模板错误求助

Hey there, let's work through that ActionView::Template::Error you're facing when trying to delete table rows with jQuery in Rails 4.2. Based on your User-Document-Hiding setup, here's a step-by-step breakdown to fix this and get your green button working as expected (deleting the row and refreshing the table):

Step 1: Lock Down Your Model Associations

First, make sure your Hiding model (and its parent models) have the correct associations set up—this avoids weird lookup errors that can bubble up to templates:

# app/models/hiding.rb
class Hiding < ActiveRecord::Base
  belongs_to :user
  belongs_to :document
  # Add validations if needed, e.g.:
  # validates :note, presence: true, length: { minimum: 3 }
end

Don't forget to update the User and Document models to mirror these associations:

# app/models/user.rb
class User < ActiveRecord::Base
  has_many :hidings, dependent: :destroy
  has_many :hidden_documents, through: :hidings, source: :document
end

# app/models/document.rb
class Document < ActiveRecord::Base
  has_many :hidings, dependent: :destroy
  has_many :hidden_by_users, through: :hidings, source: :user
end

Step 2: Fix the Hiding Controller's Destroy Action

The ActionView::Template::Error often pops up if your controller isn't handling AJAX requests correctly. Make sure your destroy action responds to JS (since we're using jQuery/AJAX) and sets the variables your template needs:

# app/controllers/hidings_controller.rb
class HidingsController < ApplicationController
  before_action :set_hiding, only: [:destroy]
  before_action :authorize_hiding_owner, only: [:destroy] # Prevent unauthorized deletes

  def destroy
    @hiding.destroy
    # Refresh the list of hidden docs for the current user (for table refresh)
    @hidden_documents = current_user.hidden_documents

    respond_to do |format|
      format.html { redirect_to hidden_documents_path, notice: 'Document is no longer hidden.' }
      format.js # This tells Rails to render destroy.js.erb
    end
  end

  private
  def set_hiding
    @hiding = Hiding.find(params[:id])
  end

  def authorize_hiding_owner
    unless @hiding.user == current_user
      redirect_to root_path, alert: "You can't delete someone else's hidden document entry."
    end
  end
end

Step 3: Update Your Table View to Send AJAX Requests

Your green button needs to trigger an AJAX request (instead of a full page reload). Use Rails' remote: true option on your link/button to enable this, and give each table row a unique ID so jQuery can target it:

# Example table in app/views/hidings/index.html.erb
<table id="hidden-documents-table" class="table">
  <thead>
    <tr>
      <th>Document Name</th>
      <th>Hide Note</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <% @hidden_documents.each do |document| %>
      <% hiding = document.hidings.find_by(user: current_user) %>
      <tr id="hiding-<%= hiding.id %>">
        <td><%= document.title %></td>
        <td><%= hiding.note %></td>
        <td>
          <%= link_to 'Unhide', hiding_path(hiding), 
                      method: :delete, 
                      remote: true, 
                      class: 'btn btn-success' %> <!-- Green button -->
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

Step 4: Write the destroy.js.erb Template

This is where the magic happens—this JS template will delete the row and refresh the table without a full page reload. Create it in app/views/hidings/destroy.js.erb:

// Fade out and remove the targeted table row
$('#hiding-<%= @hiding.id %>').fadeOut(300, function() {
  $(this).remove();
});

// Optional: Refresh the entire table body to reflect updated data
// (Use this if deleting the row affects other parts of the table)
$('#hidden-documents-table tbody').html('<%= j render partial: "hidden_documents", locals: { hidden_documents: @hidden_documents } %>');

Note: The j is shorthand for escape_javascript—it ensures your rendered HTML doesn't break the JS code.

Troubleshooting Common ActionView::Template::Error Causes

If you're still hitting the error, check these common issues:

  • Undefined variables: Make sure @hiding and @hidden_documents are set in your controller's destroy action—template errors often happen when variables are missing.
  • Partial path errors: If you're using a partial to render the table body, double-check the path (e.g., if your partial is in app/views/users/_hidden_documents.html.erb, use render 'users/hidden_documents').
  • Missing Rails UJS: Ensure you have //= require jquery_ujs in your application.js file—this is required for remote: true to work in Rails 4.2.

内容的提问来源于stack exchange,提问作者mareck_ste

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:25:35