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

Ruby on Rails创建用户时报错:No route matches [PUT] "/users"

Fixing "No route matches [PUT] "/users"" When Creating a New User in Rails

Hey there! Let's break down why you're hitting this error and how to fix it quickly. 😊

What's the Core Issue?

In Rails' RESTful routing rules, PUT requests are reserved for updating existing users (they target paths like /users/:id). But when creating a new user, you need a POST request sent to /users—which maps to the create action in your UsersController. The error means your form is incorrectly sending a PUT request to the wrong endpoint.

Common Causes & Step-by-Step Fixes

1. Ensure @userInfo is a fresh, unsaved user object

The most likely culprit is that your UsersController's new action isn't initializing a blank user instance. If @userInfo is an existing record (like one pulled from the database), form_for will automatically default to a PUT request instead of POST.

Fix your controller code:

class UsersController < ApplicationController
  def new
    # This creates a blank, unsaved user object (critical for form behavior)
    @userInfo = User.new
  end

  def create
    # Handle saving the new user with strong parameters
    @userInfo = User.new(user_params)
    if @userInfo.save
      # Redirect to the user's profile or a success page after save
      redirect_to @userInfo
    else
      # Re-render the form if validation fails (keeps user input)
      render 'new'
    end
  end

  private
  # Whitelist allowed fields to prevent mass assignment vulnerabilities
  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end
end

2. Simplify your form_for markup

You don't need to manually specify :url => users_path if @userInfo is a new object—form_for will automatically detect the correct path and HTTP method. Manually overriding the URL can lead to unexpected method mismatches.

Update your new.html.erb form:

<div class='container'>
  <div class="row">
    <div class="login col-lg-6 col-lg-offset-3">
      <%= form_for @userInfo do |f| %>
        <!-- Add your form fields here (example fields below) -->
        <div class="form-group">
          <%= f.label :name %>
          <%= f.text_field :name, class: 'form-control' %>
        </div>

        <div class="form-group">
          <%= f.label :email %>
          <%= f.email_field :email, class: 'form-control' %>
        </div>

        <div class="form-group">
          <%= f.label :password %>
          <%= f.password_field :password, class: 'form-control' %>
        </div>

        <div class="form-group">
          <%= f.label :password_confirmation %>
          <%= f.password_field :password_confirmation, class: 'form-control' %>
        </div>

        <%= f.submit "Create Account", class: 'btn btn-primary' %>
      <% end %>
    </div>
  </div>
</div>

Quick Route Verification

Your routes.rb is already correct—resources :users generates all standard RESTful routes, including the POST /users endpoint needed for creating users. You can confirm this by running rails routes in your terminal and looking for this line:

POST   /users(.:format)     users#create

That's all! Once you ensure @userInfo is a fresh instance and let form_for handle routing automatically, the error should disappear.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:42:41