Rails 5刷新Partial中Div报错:FormsController#show模板未找到
Hey there, let's break down what's going on here and fix that error you're seeing:
- Your
testzzz.html.erbworks because it's a full template that directly renders_test_partial.html.erbduring a normal page load—Rails doesn't need a special controller action to handle that straightforward render. - When you try to refresh the partial via AJAX, Rails defaults to routing that request to
FormsController#show(a common fallback for RESTful setups), but you don't have a template for that action, and you haven't set up a dedicated route/action to handle the partial refresh specifically.
Here's how to fix this step by step:
1. Add a Route for the Partial Refresh
First, tell Rails exactly where to send the AJAX request. Open your config/routes.rb and add one of these options based on your needs:
# For refreshing a partial tied to a specific form (member route) resources :forms do get 'refresh_partial', on: :member end # OR for a general partial not linked to a single form (collection route) resources :forms do get 'refresh_partial', on: :collection end # OR a simple standalone route (if you don't need RESTful context) get 'forms/refresh_partial', to: 'forms#refresh_partial'
2. Add the Controller Action
Next, create the refresh_partial action in app/controllers/forms_controller.rb to handle the request and return your updated partial. You have two solid options here:
Option 1: Render the Partial Directly (Returns Raw HTML)
This is great if you want to handle the DOM update in your frontend JS:
def refresh_partial # Define any variables your partial needs (like the time/random number) @current_time = Time.now @random_number = rand(100) # Render only the partial, no surrounding layout render partial: 'test_partial', layout: false end
Option 2: Use a Rails JS Template (Handles DOM Updates Automatically)
If you prefer to let Rails handle the DOM replacement:
def refresh_partial @current_time = Time.now @random_number = rand(100) respond_to do |format| format.js # This will look for app/views/forms/refresh_partial.js.erb end end
Then create app/views/forms/refresh_partial.js.erb with this code to update your div:
// Replace 'your-target-div-id' with the actual ID of the div holding your partial $('#your-target-div-id').html("<%= j render partial: 'test_partial' %>");
The j is shorthand for escape_javascript—it makes sure your partial's HTML is safely inserted into the JS without breaking things.
3. Update Your Frontend AJAX Request
Make sure your JS points to the correct route. For example, if you used a member route (tied to a specific form):
$('#refresh-button').on('click', function() { $.ajax({ url: '/forms/<%= @form.id %>/refresh_partial', // Match the route you set up type: 'GET', success: function(data) { // If you used Option 1, replace the div content here $('#your-target-div-id').html(data); // If you used Option 2, the JS template handles this automatically } }); });
Quick Pitfalls to Avoid
- Double-check your partial path: If
_test_partial.html.erblives inapp/views/shared, you'll need to writerender partial: 'shared/test_partial'instead of just'test_partial'. - If you use Rails UJS, you can skip custom JS entirely by using
link_to 'Refresh', form_refresh_partial_path(@form), remote: true. - Don't forget any authorization rules (like
before_action :authenticate_user!if using Devise) if your app requires them for this action.
内容的提问来源于stack exchange,提问作者Chris Mendla

