Ruby on rails 为什么带有require和permit的params不传递值来创建新记录?
我有以下资料:Ruby on rails 为什么带有require和permit的params不传递值来创建新记录?,ruby-on-rails,ruby-on-rails-4,actionview,Ruby On Rails,Ruby On Rails 4,Actionview,我有以下资料: class ArticlesController < ApplicationController before_action :find_article, only: [:edit, :update, :show, :delete] def create @article = Article.new if @article.save(article_params) flash[:notice] = "Successfully crea
class ArticlesController < ApplicationController
before_action :find_article, only: [:edit, :update, :show, :delete]
def create
@article = Article.new
if @article.save(article_params)
flash[:notice] = "Successfully created article!"
redirect_to article_path(@article)
else
flash[:alert] = "Error creating new article!"
render :new
end
end
private
def article_params
params.require(:article).permit(:title, :body)
end
end
但是,在控制器中未使用日志中显示的提交的标题值。知道为什么吗?当您通过控制台检查提交的最后一条记录的标题时,它会说什么?我该怎么做?如果您的意思是在数据库中,没有向日志输出中显示的数据库发送任何内容,那么您只是忘记了设置Article对象的属性。您不会将属性传递给
save
方法。您可以执行Article.new(Article\u params)
,也可以执行@Article.attributes=Article\u params
。然后再保存。
Started POST "/articles" for ::1 at 2017-01-01 11:30:13 -0800
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xx==", "article"=>{"title"=>"MAKEMEWORK", "body"=>""}, "commit"=>"Create Article"}
(0.3ms) BEGIN
SQL (0.4ms) INSERT INTO "articles" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2017-01-01 19:30:13.630647"], ["updated_at", "2017-01-01 19:30:13.630647"]]
(0.5ms) COMMIT
Redirected to http://localhost:3000/articles/7
Completed 302 Found in 15ms (ActiveRecord: 4.0ms)