ActionController :: ParameterMissing(param丢失或值为空:

时间:2015-03-26 03:12:38

标签: ruby-on-rails-4 controller params actioncontroller

我的控制器似乎做了它应该做的事情,但我一直收到一条我无法弄清楚的错误信息。我浏览了几个小时试图找到一个类似的问题,我确实找到了一些,并尝试了他们的建议,但由于某种原因它不适合我。

这是我的环境

user@host:~/app$ rails -v
Rails 4.2.1
user@host:~/app$ ruby -v
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]
user@host:~/app$

这是我的控制器

require 'net/http'
require 'net/https'
require 'json'
require 'uri'

class NrisController < ApplicationController
  before_action :set_nri, only: [:show, :edit, :update, :destroy]

  # GET /nris
  # GET /nris.json
  def index
    @nris = Nri.all
  end

  # GET /nris/1
  # GET /nris/1.json
  def show
  end

  # GET /nris/new
  def new
    @nri = Nri.new
  end

  # GET /nris/1/edit
  def edit
  end

  # POST /nris
  # POST /nris.json
  def create
    @nri = Nri.new
  end

  # PATCH/PUT /nris/1
  # PATCH/PUT /nris/1.json
  def update
    <snip>
  end

  # DELETE /nris/1
  # DELETE /nris/1.json
  def destroy
    @nri.destroy
    <snip>
  end

  # PATCH/PUT /nris/1
  # PATCH/PUT /nris/1.json
  def apply_config
    @nri = Nri.find(params[:id])
    <snip>
  end

  private

    def set_nri
      @nri = Nri.find(params[:id])
    end

    def nri_params
      params.require(:nri).permit(:MyParam1, :MyParam2, :MyParam3, :id)
    end
end

我也试过

  def create
    @nri = Nri.new(nri_params)
    end
  end

  def create
    @nri = Nri.create(nri_params)
    end
  end

但我不断收到此错误消息

ActionController::ParameterMissing (param is missing or the value is empty: nri):
app/controllers/nris_controller.rb:176:in `nri_params'
app/controllers/nris_controller.rb:146:in `block in apply_config'
app/controllers/nris_controller.rb:145:in `apply_config'


Rendered /home/user/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.5ms)
Rendered /home/user/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.6ms)
Rendered /home/user/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.8ms)
Rendered /home/user/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (16.3ms)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果您使用Postman或发送json作为Post请求,则需要将标题ContentType设置为application / json。