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"}
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With