NoMethodError:字符串的未定义方法“ permit”

时间:2019-02-01 22:46:16

标签: ruby-on-rails ruby ruby-on-rails-5 axios nomethoderror

我有一个带有Rails API的React SPA。我最近在React端更改了我的API请求,使其通过axios而不是jquery,并且我的axios设置为:

export default class API {
  static send(verb, resource, rawParams, callback, errcallback) {
    const params = Object.keys(rawParams).reduce((o, k) => {
      o[k] = rawParams[k];
      return o;
    }, {});

    const errorCallback = typeof errcallback === 'function' ? errcallback : () => {};

    const request = {
      method: verb,
      url: `${URL}/${resource}`
    };

    const token = window.sessionStorage.getItem('jwt');
    request.headers = {};
    if (token !== 'undefined' && token != null) {
      request.headers.Authorization = `Bearer ${token}`;
    }

    if (verb === 'GET' || verb === 'DELETE') {
      request.params = params;
      request.headers['Content-Type'] = 'application/json';
      request.headers.Accept = 'application/json';
    } else {
      request.headers['Content-Type'] = 'application/json';
      request.headers.Accept = 'application/json';
      request.data = params;
    }

    axios(request)
      .then(response => {
        if (resource === 'users/sign_in') {
          const jwt = response.headers.authorization;
          window.sessionStorage.setItem('jwt', jwt.split('Bearer ')[1]);
        } else if (resource === 'users/sign_out') {
          window.sessionStorage.removeItem('jwt');
        }
        callback(response.data);
      })
      .catch(error => {
        errorCallback(error.response.data);
      });
  }

  static baseURL() {
    return URL;
  }

  static get(resource, params, callback, errcallback) {
    API.send('GET', resource, params, callback, errcallback);
  }

  static post(resource, params, callback, errcallback) {
    API.send('POST', resource, params, callback, errcallback);
  }

  static put(resource, params, callback, errcallback) {
    API.send('PUT', resource, params, callback, errcallback);
  }

  static del(resource, params, callback, errcallback) {
    API.send('DELETE', resource, params, callback, errcallback);
  }
}

和该I创建一个请求,诸如:

API.get('user/user_auth', { validate_user: { user_type: 'gamer' } }. res => {
  ...
}

但是现在我的Rails方面在许可参数方面失败了。许可证定义为:

def user_type_params
  params.require(:validate_user).permit(:user_type)
end

每当我在Rails代码中执行类似user_type_params[:user_type]之类的操作时,都会收到如下错误:

NoMethodError (undefined method `permit' for "{\"user_type\":\"gamer\"}":String)`

即使我添加了request.headers['Content-Type'] = 'application/json';,它也将其作为字符串发送。有一些其他的方式向PARAMS发出请求之前正确地转换?

2 个答案:

答案 0 :(得分:0)

尝试类似的东西:

def user_type_params
  json = params.require(:validate_user)
  {user_type: JSON.parse(json).permit(:user_type)}
end

此外,您不一定需要使用许可证。您可以自己将这些参数列入白名单。

答案 1 :(得分:0)

事实证明,Axios转义嵌套JSON是一个已知问题:axios/issues/738

解决此问题的方法是在我的api.js中添加一个拦截器,例如:

axios.interceptors.request.use(config => {
  config.paramsSerializer = params =>
    qs.stringify(params, {
      arrayFormat: 'brackets',
      encode: false
    });

  return config;
});

这可确保在将嵌套的参数发送到Rails之前正确地对其进行转换