Search code examples
ruby-on-railsrubyactioncontroller

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


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 :)


Solution

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