简体   繁体   中英

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

so i know that there´sa 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 :)

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

当表单与现场提交,也可能出现此问题body ,如果它是你的情况变化产生了重要的参数为空params.fetch(:opinion, {}).permit(:body)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM