Reputation: 41
I'm getting the following error when I'm trying to create a new listing and I can't figure out where is issue is.
ActionController::ParameterMissing in BusinessListingsController#create
param is missing or the value is empty: businesslisting
Below is the code for business_listing_controller:
class BusinessListingsController < ApplicationController
def index
@businesslistings = BusinessListing.all
end
def new
@businesslisting = BusinessListing.new
end
def create
@businesslisting = BusinessListing.new(businesslisting_params)
if @businesslisting.save
redirect_to @businesslisting
else
render 'new'
end
end
def show
@businesslisting = BusinessListing.find(params[:id])
end
private
def businesslisting_params
params.require(:businesslisting).permit(:title, :price, :city, :industry)
end
end
view/business_listing/:
<div class="row">
<div class="col-md-6">
<%= form_for(@businesslisting) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.label :city %>
<%= f.text_field :city %>
<%= f.label :industry %>
<%= f.text_field :industry %>
<%= f.submit "Create a Listing", class: "btn btn-primary" %>
<% end %>
Thanks!
Upvotes: 3
Views: 2592
Reputation: 28800
I had this challenge when working on a Rails 6 API in Ubuntu 20.04.
I had a Schools
Resource (Controller, Model and Route) and I needed to create a School using Postman. The Schools
resource has 2 params (name and alias). So I used the Params section to pass it, but I get the error:
app/controllers/schools_controller.rb:49:in `school_params'
app/controllers/schools_controller.rb:18:in `create'
Started POST "/schools" for ::1 at 2020-12-09 03:40:29 +0100
Processing by SchoolsController#create as */*
Parameters: {"name"=>"The Prince School", "alias"=>"The Prince"}
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms | Allocations: 122)
ActionController::ParameterMissing (param is missing or the value is empty: school):
Here's how I fixed it:
The issue was that the application was expecting another parameter called school
which will encapsulate other parameters, according to my school_params
in the SchoolsController
:
def school_params
params.require(:school).permit(:name, :alias)
end
Solution 1:
I defined the Content-Type
as application/json
in the Header section, then passed the parameters in the Body section of Postman as raw JSON object using this:
{
"school": {
"name": "The Prince School",
"alias": "The Prince"
}
}
And it worked fine.
Solution 2:
I submitted the request using form-data by clicking on the Body section of Postman and selecting form-data. SO the request looked this way:
school[name]:The Prince School
school[alias]:The Prince
Resources: Testing Rails 5 API with Postman
That's all.
I hope this helps
Upvotes: 3
Reputation: 1434
The problem is that you are not following Rails naming conventions. You have your class as BusinessListing
, but you are naming instance variables and param references as businesslisting
. BusinessListing
(camel case) is essentially two words, which should be matched as snake case when using instance variable names: business_listing
The same would go for your param name. When you pass a model object into form_for
, Rails uses the class name to determine the param name, so I'll bet the actual param that's coming across is business_listing
, not businesslisting
Just change all occurrences of businesslisting
to business_listing
(and businesslistings
to business_listings
) throughout your code, and you should no longer experience this issue.
The most important change would be here:
params.require(:business_listing).permit(:title, :price, :city, :industry)
This by itself should solve your current issue, but I strongly encourage you to follow the convention throughout your code to avoid further issue and confusion.
Upvotes: 3