Reputation: 51
so i know that there´s a lot of questions like that, but none had answered my questions and solved my problem. So, I am with this problem for about a week and I can´t solve it! I am really new to ruby on rails, but i´ve tried tried everything. I have this (ActionController::ParameterMissing: param is missing or the value is empty:opinion) and I don´t know how to fix it. There´s all the code (I am newbie so it is really simple) :
Opinion Controller :
class OpinionsController < ApplicationController
def new
@opinion = Opinion.new
end
def create
@opinion = Opinion.new(opi_params)
@opinion.save
redirect_to @opinion
end
def show
@opinion = Opinion.find(params[:id])
end
private
def opi_params
params.require(:opinion).permit(:body)
end
end
New :
<h1>Opinions</h1>
<%= form_for :opinion do |f| %>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<br>
<%= f.submit "Create an option" %>
<% end %>
DB :
class CreateOpinions < ActiveRecord::Migration
def change
create_table :opinions do |t|
t.string :body
end
end
end
Show :
<h1>Your Opinions:</h1>
<div>
<%= @opinion.body %>
</div>
PLEASE HELP ME! I am getting crazy because i cannot solve it! Thanks :)
Upvotes: 3
Views: 4073
Reputation: 1
I recommend dropping a debugger like pry
or byebug
right between def create
and @opinion = Opinion.new(opi_params)
and seeing what params
is coming in. You might be missing opinion
, which will trigger the ParameterMissing
error.
Also, are you getting this error through the browser or through your test? If you are getting this error from your test, it might be your set up.
In the meantime, try params.permit(:body)
in your opi_params
method.
Upvotes: 0
Reputation: 53
This issue may also occur when the form is submitted with the field body
empty if it is your case change the strong parameters to
params.fetch(:opinion, {}).permit(:body)
Upvotes: 0
Reputation: 983
<h1>Opinions</h1>
<%= form_for @opinion do |f| %>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<br>
<%= f.submit "Create an option" %>
<% end %>
Change :opinion
to the instance variable @opinion
.
Upvotes: 2