Ruby on Rails提交表单后提示UsersController无show动作,redirect_to失效求助
Hey there, let's troubleshoot why your form submission is throwing that "The action 'show' could not be found for UsersController" error and ignoring your redirect_to code. I've run into this exact issue a handful of times—here are the most likely fixes to try:
1. First, Check Your Form's Submission Path (And That .rb Typo!)
Wait, you mentioned your view file is users/new.html.rb—that's probably a typo! Rails views use .erb (or .haml, etc.) extensions, so it should be new.html.erb. If the file extension is wrong, Rails might not render the form correctly, leading to wonky submission paths.
Next, make sure your form is pointing to the create route, not the show route. Open up your view and check the form tag:
# Correct form for creating a new user <%= form_with(model: @user, local: true) do |form| %> # Your form fields here <% end %>
This should generate a POST request to /users (the create endpoint). But if your new action in users_controller.rb is accidentally initializing an existing user instead of a new one, the form will submit to /users/:id (the show/update path) instead:
# Wrong! This makes the form target an existing user's show path def new @user = User.find(params[:id]) end # Correct! Initializes a blank new user def new @user = User.new end
If the form is sending a request to /users/:id, that's why Rails is looking for the show action (which you might not have defined).
2. Fix Your create Action Logic
The most common reason redirect_to fails is because the code doesn't exit the action after the redirect, or there's an unhandled error before it runs. Let's structure your create action properly:
def create @user = User.new(user_params) if @user.save # Add a notice for feedback, and use return to stop execution redirect_to root_path, notice: "User created successfully!" return else # If save fails, re-render the new form with errors render :new, status: :unprocessable_entity end end
Without return (or proper if/else branching), any code after redirect_to could run and override the redirect. Also, make sure your user_params method is correctly permitting all required fields—if validation fails and you don't handle it, Rails might try to render a non-existent create template, leading to unexpected behavior.
3. Verify Your Routes
Check config/routes.rb to ensure you have the right routes set up. If you're using RESTful routes:
# This generates all standard routes (new, create, show, edit, update, destroy) resources :users # If you don't need the show action, restrict the routes: resources :users, only: [:new, :create, :index]
If you have resources :users but haven't defined a show action in your controller, any request to /users/:id will throw that "action not found" error. So either add the show action, or restrict your routes to exclude it—but first fix the form's submission path so it doesn't hit that route.
4. Check for Interfering Before Actions or Errors
Take a look at any before_action callbacks in your UsersController. If you have a callback that redirects or raises an error before the create action completes, it could block your redirect. For example:
# If this callback redirects without allowing the create action to run, it'll cause issues before_action :require_admin, only: [:create]
Make sure these callbacks are properly configured to let the create action execute when it should.
5. Use the Server Logs to Diagnose the Exact Issue
This is non-negotiable—fire up your Rails server with rails s, submit the form, and look at the terminal logs. You'll see exactly what request is being sent (method, path), what parameters are passed, and where the controller is getting stuck. For example:
Started POST "/users/1" for 127.0.0.1 at 2024-05-20 10:30:00 -0400
Processing by UsersController#show as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@example.com"}, "id"=>"1"}
AbstractController::ActionNotFound (The action 'show' could not be found for UsersController):
This log tells you the form is submitting to /users/1 (the show path) instead of /users (create path)—so you know to fix the form's initialization in the new action.
内容的提问来源于stack exchange,提问作者Aramis Delgado

