Home > Net >  Rails // ActionController::ParameterMissing error
Rails // ActionController::ParameterMissing error

Time:04-08

I am getting an ActionController::ParameterMissing in FarmersController#create error. I am just doing a basic user login setup. A lot of the similar help out there suggests I may have a spelling, or a plural vs singular mix up. I can not seem to find any such discrepancy.

The error discription: param is missing or the value is empty: farmer

Log:

Started POST "/signup" for ::1 at 2022-04-04 17:02:59 -0400 Processing by FarmersController#create as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "email"=>"[email protected]", "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

Model:

class Farmer < ApplicationRecord
  has_secure_password
end

Schema:

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

View:

<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 %>

Controller:

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

Rendered HTML form:

<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>

Thank you in advance. Much appreciated.

CodePudding user response:

Because you are using has_secure_password, please check if you have installed Bcrypt gem. In your Gemfile:

gem 'bcrypt', '~> 3.1.7'

CodePudding user response:

I went back and reset everything. Somewhere along the way, the error stopped getting triggered. To the best of my knowledge, the code above is more or less correct for what I want to do.

  • Related