简体   繁体   English

Rails/React 不允许的参数

[英]Rails/React Unpermitted Parameters

I have been tackling this issue for the past few days.在过去的几天里,我一直在解决这个问题。 I keep getting the error below regarding unpermitted parameters:我不断收到以下关于未经允许的参数的错误:

Parameters: {"category_id"=>6, "name"=>"", "ingredients"=>"", "chef_name"=>"", "origin"=>"", "recipe"=>{"name"=>"", "ingredients"=>"", "chef_name"=>"", "origin"=>"", "category_id"=>6}}
Unpermitted parameter: :recipe
   (0.1ms)  begin transaction
  ↳ app/controllers/recipes_controller.rb:19:in `create'

As you notice, it keeps making a recipe within a recipe hence ':recipe' being a unpermitted parameter.It's basically injecting an extra parameter called recipe somewhere and I don't know how to find this one.正如您所注意到的,它不断在配方中制作配方,因此 ':recipe' 是一个不允许的参数。它基本上是在某处注入一个名为 recipe 的额外参数,我不知道如何找到这个参数。

Here is my create action in my recipe controller since I am trying to create a recipe这是我的食谱 controller 中的创建操作,因为我正在尝试创建食谱

def create
        recipe = Recipe.create(recipe_params)
        if recipe.save
            render json: recipe
        else
            render json: { error: "Couldn't save" }
        end
    end
    
    private

    def recipe_params
        params.permit(:category_id,:name,:ingredients,:chef_name,:origin,category:[:id,:category])
    end

and here is my post action that is sending the request to the rails api这是我将请求发送到轨道 api 的发布操作

export const postRecipes = (recipe)=>{
    const BASE_URL = `http://localhost:10524`
    const RECIPES_URL =`${BASE_URL}/recipes`

    
    const config = {
        method: "POST",
        body:JSON.stringify(recipe),
        headers: {
        "Accept": "application/json",
        "Content-type": "application/json"
     }
    }

   
    //category field
    return(dispatch)=>{
    fetch(RECIPES_URL,config)
    .then(response => response.json())
    .then(resp => {
        dispatch({
            type: 'Add_Recipe',
            payload:{
                
                name: resp.name,
                ingredients: resp.ingredients,
                chef_name: resp.chef_name,
                origin: resp.origin,
                category: resp.category
            
            }
        })
    })
    
      .catch((error) => console.log.error(error))

    }
    
    
}

Here is my reducer for adding recipes这是我添加食谱的减速器

export default function manageRecipes(state={
    recipes:[],
    category_id:[],
}, action){


    switch(action.type){
        case 'Add_Recipe':
            const recipe = {
                
                name: action.name,
                ingredients: action.ingredients,
                chef_name: action.chef_name,
                origin: action.origin,
                category: action.category
                
            }

            return{
                ...state,
                recipes: [...state.recipes, recipe],
            }

       default:
            return state
        
    }
}


Is anyone able to help spot how this happens?有没有人能够帮助发现这是如何发生的?

Try adding :recipe in params.permit or params.require(:recipe).permit(...your_other_code)尝试在params.permitparams.require(:recipe).permit(...your_other_code)中添加:recipe

Credit to Dcangulo归功于 Dcangulo

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

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