创建新对象时出现强参数错误

Strong parameter error when creating a new object

这就是我在这里要做的。我从用户控制器调用功能测试。在测试函数中,我初始化了一个新的文章对象,就像这样 article = new Article。但这给了我错误

ArgumentError in UsersController#create
When assigning attributes, you must pass a hash as an argument. 

我有以下代码

users_controller

def create
 User.test
end

private
def user_params
  params.require(:user).permit(:name, articles_attributes:[:content)
end

用户模型

class User

def self.test
  article = new Article
  article.attributes = {"content"=>"this is some sample content"}
  article.save
end

end

我知道这个问题已被问及很多次,但我找不到与我的问题相匹配的任何内容或解决方案。所以请告诉我如何在用户模型调用中保存文章对象。

article = new Article 工作情况如何?我懂了。它调用 Class#new 然后传入 Class 自己的定义......所以它编译。但它仍然没有按照你的想法去做。尝试 article = Article.new

此外,您在用户模型中的分配与控制器 user_params 清理参数的方法无关