【发布时间】:2019-12-05 10:37:57
【问题描述】:
这让我很生气。
我有一个相对简单的设置,但由于某种原因,我试图通过的主要模型是不允许的。
日志如下所示:
Started POST "/statements" for ::1 at 2019-12-05 23:19:37 +1300
Processing by StatementsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"O4Ye+SFx7kGUceDKJlXs8A0FlH/e7r08xpLpDuICHmgAqaxw/e0UV8AR1pUZGhGlWKOcePtt3LXM7rsXXNTbGw==", "statement"=>{"content"=>"…all people are created equal.", "tags"=>"politics, equality", "parent_id"=>"dagCzw"}, "commit"=>"Submit variant and agree"}
Unpermitted parameters: :authenticity_token, :statement
statement 是我想要做的一件事——但这是不允许的,并且创建的语句总是用 nil 属性填充。
感谢您对此的任何帮助!
删除希望不相关的部分:
statement.rb
class Statement < ApplicationRecord
new.html.erb
<%= render partial: "statements/partials/new_statement_form",
locals: {statement: @statement, parent: @parent} %>
_new_statement_form.html.erb
<%= form_for(statement) do |f| %>
statements_controller.rb
def new
Rails.logger.debug "\n\n-------- new ---------\n\n"
@statement = Statement.new
@content = "…"
if params[:parent].present?
@parent = Statement.find_by_hashid(params[:parent])
@content += "#{@parent.content}."
end
end
.
.
.
def create
if create_params[:parent_id]
Rails.logger.debug "\n\nCreate child\n\n"
@statement = Statement.find_by_hashid(create_params[:parent_id]).children.create(create_params[:statement])
else
Rails.logger.debug "\n\nCreate root\n\n"
@statement = Statement.new(create_params[:statement])
end
.
.
.
def create_params
params.require(:statement).permit(:content, :tags, :parent_id)
end
【问题讨论】:
-
我猜应该是
children.create(create_params)。 -
@MarekLipka 是的,我明白了——但它仍然不能改变该声明不被允许的事实。谢谢!
-
好的,“它不起作用”到底是什么意思?它会抛出错误吗?
-
我们看不到您的模型关联,但看起来问题可能出在
@statement = Statement.find_by_hashid(create_params[:parent_id]).children.create(create_params[:statement])。阅读嵌套关联的官方指南,看看是否有帮助。 api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/…
标签: ruby-on-rails strong-parameters