Rails控制器Create方法失效问题求助
Hey there, let's break down why your user creation form isn't working as expected. Based on the code you shared, here are the most common issues to check:
1. Missing Password Parameters (Likely Culprit)
Looking at your user_params method, you're only permitting fields like firstname, surname, and email—but most Rails user models require password and password confirmation fields (either via Devise or custom validations). If your User model has validations for password presence/confirmation, the save will fail silently because those parameters aren't being passed through.
Fix this by updating your user_params to include password fields (adjust based on your model's actual attributes):
def user_params params.require(:user).permit(:firstname, :surname, :nickname, :email, :timezone, :password, :password_confirmation ) end
Also make sure your form includes these password fields (if they're missing):
<%= form.label :password %> <%= form.password_field :password %> <%= form.label :password_confirmation %> <%= form.password_field :password_confirmation %>
2. Check Model Validations
Even if you have the right parameters, your User model might have other validations that aren't being met. For example:
validates :email, presence: true, uniqueness: truevalidates :firstname, presence: true
To see exactly what's failing, add error messages to your new.html.erb template so you can see why the save is rejected:
<% if @user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> <ul> <% @user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
3. Inspect Server Logs for Clues
When you submit the form, check your Rails server terminal output. Look for lines like:
- The parameters being sent (confirm the
userhash includes all expected fields) - Any
ActiveRecord::RecordInvaliderrors or validation messages
For example, you might see something like:
Started POST "/users" for ::1 at 2024-05-20 10:00:00 +0200 Processing by UsersController#create as HTML Parameters: {"authenticity_token"=>"[TOKEN]", "user"=>{"firstname"=>"John", "surname"=>"Doe", "email"=>"john@example.com"}, "commit"=>"Create User"} (0.1ms) BEGIN User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "john@example.com"], ["LIMIT", 1]] (0.1ms) ROLLBACK Rendering users/new.html.erb within layouts/application Rendered users/new.html.erb within layouts/application (1.2ms) Completed 200 OK in 10ms (Views: 8.5ms | ActiveRecord: 0.4ms)
The ROLLBACK line confirms the save failed, and the error messages you added earlier will tell you exactly why.
4. Confirm Form Structure
Double-check that your form is correctly built with form_with(model: @user) (or form_for in older Rails versions) so it submits parameters under the :user key. If the form isn't scoped to @user, your params.require(:user) will throw an error (which you'll see clearly in the server logs).
内容的提问来源于stack exchange,提问作者Mike.Whitehead

