【问题标题】:Ruby: Form not submit - ActionController::Parameters permitted: falseRuby:表单未提交 - ActionController::Parameters allowed: false
【发布时间】:2023-04-11 00:37:01
【问题描述】:

在我的项目中添加一些 gem 后,我的表单不再创建条目。

我添加了deviseactive admin,之后我的表单提交停止工作。 当我尝试调试时,我收到了这条消息:

# posts_controller.rb

def create
  render plain: params[:post].inspect
end

输出:

<ActionController::Parameters {"firstname"=>"Andreas", "lastname"=>"", "street"=>"", "city_code"=>"", "city"=>"", "state"=>"", "country"=>""} permitted: false>

这是生产代码:

def create
  @post = Post.new(post_params)

  if(@post.save)
    redirect_to @post
  else
    render 'new'
  end
end

它提交表单但不会生成数据库条目。 有什么想法吗?

【问题讨论】:

  • render 'new' 行之前检查@post.errors,看看是否有验证错误。
  • 我只是把@post.errors 放在render 的那一行?这给了我一个undefined method "errors" for nil:NilClass。或者我该怎么做?服务器日志对此有帮助吗? Started POST "/posts" for 127.0.0.1 at 2018-02-15 16:34:16 +0100 Processing by PostsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"dD230A0PIRSM3Z0nnMU14vZbB610CDOFc80OzHNhZe7HcfEha0LzHUZCCx3xUCxujlKK5AXapr/ahpnMZ/RB5A==", "post"=&gt;{"firstname"=&gt;"Test" }, "commit"=&gt;"Save Post"} Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms)
  • else 块内添加一行,在您的render 语句之前:puts @post.errors.messages,然后在您尝试创建新帖子时查看您的服务器控制台。 guides.rubyonrails.org/…
  • 可以把 post_params 方法放上去吗?

标签: ruby-on-rails forms actioncontroller


【解决方案1】:

感谢您的帮助! 我不小心在我的控制器中需要一个额外的字段,该字段不存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多