Search code examples
ruby-on-railsruby-on-rails-4

Rails: ActionController::ParameterMissing param is missing or the value is empty:


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!


Solution

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