简体   繁体   中英

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

I have a model called 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. 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.

Based on the route I thought the required parameters were user_id and 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:

  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). 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. All that is required is user_id and global_product_id, which are in the failed request.

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.

You can fix link_to to be like:

<%= 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" %>

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