Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 error "param is missing or the value is empty:" in create

I've looked at several tutorials, the ruby guides, and several stackoverflow questions. I tried first with simple_form and now the old fashioned way and can't figure out why the params aren't being passed.

Controller:

def new
  @topgem = Topgem.new
end

def create 
  @topgem = Topgem.new(topgem_params)

  if @topgem.save
    redirect_to @topgem
  else
    render 'new'
  end

...

 private
    def topgem_params
      params.require(:name).permit(:url, :description, :downloads, :last_updated)
    end

Model:

class Topgem < ActiveRecord::Base

  has_many :votes
  has_many :users, through: :votes

  validates :name, presence: true, uniqueness: true, :length => {
    :minimum =>2,
    :maximum =>50}

  validates :url, presence: true
  validates :description, presence: true 
  validates :downloads, numericality: { only_integer: true }
end

new.html.erb

<%= form_for(@topgem) do |f| %>


  <%= f.label :name %>:
  <%= f.text_field :name %><br />

  <%= f.label :url %>:
  <%= f.text_field :url %><br />

   <%= f.label :description %>:
  <%= f.text_field :description %><br />

   <%= f.label :downloads %>:
  <%= f.number_field :downloads %><br />


  <%= f.submit %>
<% end %>

The error I am getting:

ActionController::ParameterMissing at /topgems
param is missing or the value is empty: name

here are select instance variables:

Instance Variables

@_action_has_layout 
true

@_routes    
nil

@_headers   
{"Content-Type"=>"text/html"}

@_status    
200

@_params    
{"utf8"=>"✓", "authenticity_token"=>"Gx/UwvcvWZYWAUHxWGYlUQB/PNNUniBpCjlM1WEHAm+luYl94Kky5Ae9Ur40YVtrN2ebEEX8C0G3Cewu/SJSow==", "topgem"=>{"name"=>"bfgf", "url"=>"dd", "description"=>"ff", "downloads"=>"343"}, "commit"=>"Create Topgem", "controller"=>"topgems", "action"=>"create"}
like image 682
jeffhale Avatar asked Dec 24 '22 22:12

jeffhale


1 Answers

You have required params[:name], but the actual params are params[:topgem][:name].

Change your topgem_params method to

params.require(:topgem).
  permit(
    :name,
    :url,
    :description,
    :downloads,
    :last_updated
  )
like image 140
messanjah Avatar answered Apr 13 '23 00:04

messanjah