简体   繁体   English

参数缺失或值为空-验证在请求中发送

[英]Param is missing or the value is empty - validations are sent in request

I have a model called Wish: 我有一个名为Wish的模型:

class Wish < ApplicationRecord
  belongs_to :user
  has_one :global_product

  validates :global_product_id, presence: true
  validates :user_id, presence: true
end

Here are the wish routes: 以下是愿望路线:

            user_wishes GET    /users/:user_id/wishes(.:format)                                wishes#index
                        POST   /users/:user_id/wishes(.:format)                                wishes#create
          new_user_wish GET    /users/:user_id/wishes/new(.:format)                            wishes#new
         edit_user_wish GET    /users/:user_id/wishes/:id/edit(.:format)                       wishes#edit
              user_wish GET    /users/:user_id/wishes/:id(.:format)                            wishes#show
                        PATCH  /users/:user_id/wishes/:id(.:format)                            wishes#update
                        PUT    /users/:user_id/wishes/:id(.:format)                            wishes#update
                        DELETE /users/:user_id/wishes/:id(.:format)                            wishes#destroy

Once the user creates a global_product, they are directed to its show page. 用户创建global_product之后,他们将被定向到其显示页面。 From there I created a button to create a wish. 从那里我创建了一个按钮来创建一个愿望。 Global_product_id is a foreign key on the Wish table, so they're creating a wish out of the global_product. Global_product_id是“愿望”表上的外键,因此他们从global_product中创建了一个愿望。

Based on the route I thought the required parameters were user_id and global_product_id: 基于路由,我认为所需的参数是user_id和global_product_id:

<%= link_to 'Add this product to my wish list', user_wishes_path(:global_product_id => @global_product.id,
                                                                 :user_id => current_user.id),
                                                                 :method=> :post,
                                                                 class: "btn btn-default" %>

In the wish controller, here is the create method and wish_params: 在wish控制器中,这是create方法和wish_params:

  def create
    @wish = Wish.new(wish_params)
    @wish.save
    if @wish.save
      redirect_to 'index'
    else
      render 'new'
    end
  end

  def wish_params
    params.require(:wish).permit(:global_product_id, :user_id)
  end

Here is the exact error in my last request: 这是我上次请求中的确切错误:

Parameters: {"authenticity_token"=>"y1vzGG5+CoyGS4XAFJ6sQPlD5XyNRfSMA14r4lWsUh3N1FA8Uz/Q7HNUMWOpsPQyBILgpjVxJCM552757yNsjw==", "global_product_id"=>"25", "user_id"=>"1"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)

I'm not sure what is wrong with this request as in rails console I am able to create a new wish using Wish.create(user_id: 1, global_product_id: 25). 我不确定此请求出了什么问题,因为在Rails控制台中,我可以使用Wish.create(user_id:1,global_product_id:25)创建一个新的愿望。 I had tried passing in wish and global_product into my link_to request but same error and it doesn't look like I would need those. 我曾尝试将wish和global_product传递到我的link_to请求中,但存在相同的错误,并且看起来我不需要这些错误。 All that is required is user_id and global_product_id, which are in the failed request. 所需的只是user_id和global_product_id,它们在失败的请求中。

You are sending your params as: 您将参数发送为:

{"global_product_id"=>"25", "user_id"=>"1"}

While your code: 而您的代码:

params.require(:wish).permit(:user_id, :global_product_id)

expects it to be: 期望它是:

{"wish" => {"global_product_id"=>"25", "user_id"=>"1"}}

So you need to fix either your wish_params method or your link_to call to match. 因此,您需要修复wish_params方法或link_to调用以进行匹配。

You can fix link_to to be like: 您可以将link_to为:

<%= link_to 'Add this product to my wish list', 
             user_wishes_path(user_id: current_user.id, 
                              wish: {
                                global_product_id: @global_product.id,
                                user_id: current_user.id
                              }),
             method: :post,
             class: "btn btn-default" %>

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

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