如何使用 Rails 向强参数添加新项目?

问题描述

Rails 版本:5

我想从前端向 rails 发布一个名为 urls 的新项目。

export function printPosts(params) {
  const endpoint = `v1/posts/print`
  return http.post(endpoint,params).then(({ data }) => data)
}

但是在 post 对象中,没有命名为 urls 的项目。 像 idtitlebodyurl 之类的东西。

当我发布到这个端点时,日志显示

<ActionController::Parameters {"urls"=>"https://www.example.com/post/url/1\nhttps://www.example.com/post/url/2","format"=>:json,"controller"=>"v1/posts","action"=>"print","post"=>{}} permitted: false>
undefined method `strip' for 1:Integer

permitted: false 可能意味着 urls 不允许发送。

我将它添加到 rails 的 app/controllers/v1/posts_controller.rb 控制器

private

def post_params
  params.permit(
    :urls
  )
end

然后打印它的值:

def print
  p post_params.to_h.symbolize_keys
end

显示

<ActionController::Parameters {"urls"=>"https://www.example.com/post/url/1\nhttps://www.example.com/post/url/2","post"=>{}} permitted: false>
Unpermitted parameters: :format,:post
{:urls=>"https://www.example.com/post/url/1\nhttps://www.example.com/post/url/2"}
undefined method `strip' for 1:Integer

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...