Rails // ActionController::ParameterMissing 错误

Rails // ActionController::ParameterMissing error

我收到 ActionController::ParameterMissing in FarmersController#create 错误。我只是在做一个基本的用户登录设置。很多类似的帮助表明我可能有拼写错误,或者是复数与单数的混淆。我似乎找不到任何此类差异。

错误描述: param is missing or the value is empty: farmer

日志:

Started POST "/signup" for ::1 at 2022-04-04 17:02:59 -0400 Processing by FarmersController#create as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "email"=>"one@one.com", "username"=>"one", "password"=>"[FILTERED]", "commit"=>"Sign Up"} Cart Load (0.5ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]] ↳ app/controllers/application_controller.rb:11:in `current_cart' Completed 400 Bad Request in 6ms (ActiveRecord: 0.5ms | Allocations: 1061

型号:

class Farmer < ApplicationRecord
  has_secure_password
end

架构:

create_table "farmers", force: :cascade do |t|
  t.string "username"
  t.string "email"
  t.string "password_digest"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
end

查看:

<h1>New Farmer</h1>
<hr>
<%= form_with model: @farmer do |f| %>
   <%= f.label :email %>
   <%= f.text_field :email %><br>
   <%= f.label :username %>
   <%= f.text_field :username %><br>
   <%= f.label :password %>
   <%= f.password_field :password %><br>
   <%= f.submit 'Sign Up' %>
<% end %>

控制器:

class FarmersController < ApplicationController

   def new
      @farmer = Farmer.new
   end

   def create
      farmer = Farmer.new(farmer_params)
      if farmer.save
         session[:farmer_id] = farmer.id
         redirect_to home_path   
      else
         render :new
      end
   end


   private

   def farmer_params
      params.require(:farmer).permit(:username, :email, :password)
   end

end

渲染的 HTML 形式:

<form action="/farmers" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="kI-3-ARDjvCc5Cm0qk1VgFJbDeXyzqTcDphG-bTQsWK5WIy8wRaWwhOM2uWEDZCr3AHcVgKMHusqRkhwA9nZew" autocomplete="off">
   <label for="farmer_email">Email</label>
   <input type="text" name="farmer[email]" id="farmer_email"><br>
   <label for="farmer_username">Username</label>
   <input type="text" name="farmer[username]" id="farmer_username"><br>
   <label for="farmer_password">Password</label>
   <input type="password" name="farmer[password]" id="farmer_password"><br>
   <input type="submit" name="commit" value="Sign Up" data-disable-with="Sign Up">
</form>

提前谢谢你。非常感谢。

因为您正在使用has_secure_password,请检查您是否安装了Bcryptgem。在你的 Gemfile 中:

gem 'bcrypt', '~> 3.1.7'

我回去重置了一切。在此过程中的某个地方,错误不再被触发。据我所知,上面的代码或多或少对我想做的是正确的。