简体   繁体   English

Ruby on rails 控制器创建新参数

[英]Ruby on rails controller create new params

In create method in controller i have:在控制器的创建方法中,我有:

def create
      @monitoring_group = MonitoringGroup.new(monitoring_group_params)
        if @monitoring_group.save
          payload = {
              value: @monitoring_groups,
              status: 201
          }
          render json: @monitoring_group, status: :created#, location: @monitoring_group
        else
          payload = {
              error: "One or more monitorings do not belong to this account",
              status: 400
          }
          render :json => payload, :status => :bad_request
        end
    end

And

 def monitoring_group_params
   params.require(:monitoring_groups).permit(:name, monitoring: [])
 end

I have the answer when i run the create当我运行 create 时我有答案

> #<ActionController::ParameterMissing: param is missing or the value is empty: monitoring_groups>

What i do to this create ?我对这个创造做了什么?

What does the request look like?请求是什么样的?

The action expects an object like so:该操作需要一个像这样的对象:

{
  "monitoring_groups": {
    "name":"Some name",
    "monitoring":[]
  }
}

Are you adding the outer monitoring_groups key?您是否添加了外部 monitoring_groups 键?

Also look at the development.log -> it will log the incoming request.还要查看 development.log -> 它将记录传入的请求。

You have params.require(:monitoring_groups) .你有params.require(:monitoring_groups) This means that the params object expects data nested under the :monitoring_groups key.这意味着 params 对象需要嵌套在:monitoring_groups键下的数据。

If the request is coming from an HTML form, you should have something like this there:如果请求来自 HTML 表单,您应该在那里有这样的东西:

form_for MonitoringGroup.new do |form|
  ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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