Rails 创建 post 对象的方法无效
Rails create method to post object not working
我刚开始使用 rails。我创建了一个文章 posting 表单,当我单击 post 按钮时,它会将我带到文章页面,但它不是 post 我输入的内容。
routes.rb
:
get 'articles/new' => 'articles#new'
post 'articles' => 'articles#create'
articles_controller.rb
:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to '/articles'
else
render 'new'
end
end
private
def article_params
params.required(:article).permit(:content)
end
end
`articles/new.html.erb`:
<div class="create">
<div class="container-fluid">
<%= form_for @article do |t| %>
<%= t.text_field :title, :placeholder => 'Title', :class => 'article_title' %>
<%= t.text_area :body, :placeholder => 'Article', :class => 'article_body' %>
<%= t.submit 'Post', :class => 'btn-btn-article' %>
<% end %>
</div>
</div>
服务器日志:
Started POST "/articles" for 127.0.0.1 at 2015-10-29 00:43:30 -0400
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"c49EXOKfsDP6l2xuquvmUncgTAvXIXpylgx5qK+LvCSrPFvuS37zgR9Cv/BDVwMkCFSYcaWbM+3+qpZazQby8g==", "article"=>{"title"=>"Test", "body"=>"Tsting"}, "commit"=>"Post"}
Unpermitted parameters: title, body
[1m[36m (0.1ms)[0m [1mBEGIN[0m
[1m[35mSQL (0.2ms)[0m INSERT INTO `articles` (`created_at`, `updated_at`) VALUES ('2015-10-29 04:43:30', '2015-10-29 04:43:30')
[1m[36m (11.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/articles
Completed 302 Found in 14ms (ActiveRecord: 11.3ms)
Started GET "/articles" for 127.0.0.1 at 2015-10-29 00:43:30 -0400
Processing by ArticlesController#index as HTML
Rendered articles/index.erb within layouts/application (0.0ms)
Completed 200 OK in 32ms (Views: 31.5ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:17 -0400
Processing by HomeController#home as HTML
Rendered home/home.erb within layouts/application (0.5ms)
Completed 200 OK in 31ms (Views: 30.1ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:21 -0400
Processing by HomeController#home as HTML
Rendered home/home.erb within layouts/application (0.2ms)
Completed 200 OK in 30ms (Views: 29.1ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:22 -0400
Processing by HomeController#home as HTML
Rendered home/home.erb within layouts/application (0.6ms)
Completed 200 OK in 96ms (Views: 94.8ms | ActiveRecord: 0.0ms)
Started GET "/articles/new" for 127.0.0.1 at 2015-10-29 00:51:29 -0400
Processing by ArticlesController#new as HTML
Rendered articles/new.erb within layouts/application (1.0ms)
Completed 200 OK in 86ms (Views: 85.2ms | ActiveRecord: 0.0ms)
Unpermitted parameters: title, body
您应该将 article_params
更改为以下
def article_params
params.require(:article).permit(:title, :body)
end
所以,您的服务器日志告诉您问题所在:
Unpermitted parameters: title, body
您必须将 title
和 body
属性列入白名单,因为动作控制器参数在被列入白名单之前禁止用于活动模型批量分配。
变化:
def article_params
params.required(:article).permit(:content)
end
收件人:
def article_params
params.require(:article).permit(:title, :body)
end
此外,请查看 Strong Parameters
的官方 Rails 文档
更新
我建议您在 routes
文件中使用 RESTful route:
resources :articles
那么,你会有这条路线:
articles GET /articles(.:format) articles#index
然后,在控制器的创建操作中,您可以使用 articles_path
:
def create
@article = Article.new(article_params)
if @article.save
redirect_to articles_path
else
render 'new'
end
end
它在你的日志中说 -
Unpermitted parameters: title, body
将您的 article_params
方法更改为
def article_params
params.require(:article).permit(:title, :body, :content)
end
我刚开始使用 rails。我创建了一个文章 posting 表单,当我单击 post 按钮时,它会将我带到文章页面,但它不是 post 我输入的内容。
routes.rb
:
get 'articles/new' => 'articles#new'
post 'articles' => 'articles#create'
articles_controller.rb
:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to '/articles'
else
render 'new'
end
end
private
def article_params
params.required(:article).permit(:content)
end
end
`articles/new.html.erb`:
<div class="create">
<div class="container-fluid">
<%= form_for @article do |t| %>
<%= t.text_field :title, :placeholder => 'Title', :class => 'article_title' %>
<%= t.text_area :body, :placeholder => 'Article', :class => 'article_body' %>
<%= t.submit 'Post', :class => 'btn-btn-article' %>
<% end %>
</div>
</div>
服务器日志:
Started POST "/articles" for 127.0.0.1 at 2015-10-29 00:43:30 -0400
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"c49EXOKfsDP6l2xuquvmUncgTAvXIXpylgx5qK+LvCSrPFvuS37zgR9Cv/BDVwMkCFSYcaWbM+3+qpZazQby8g==", "article"=>{"title"=>"Test", "body"=>"Tsting"}, "commit"=>"Post"}
Unpermitted parameters: title, body
[1m[36m (0.1ms)[0m [1mBEGIN[0m
[1m[35mSQL (0.2ms)[0m INSERT INTO `articles` (`created_at`, `updated_at`) VALUES ('2015-10-29 04:43:30', '2015-10-29 04:43:30')
[1m[36m (11.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/articles
Completed 302 Found in 14ms (ActiveRecord: 11.3ms)
Started GET "/articles" for 127.0.0.1 at 2015-10-29 00:43:30 -0400
Processing by ArticlesController#index as HTML
Rendered articles/index.erb within layouts/application (0.0ms)
Completed 200 OK in 32ms (Views: 31.5ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:17 -0400
Processing by HomeController#home as HTML
Rendered home/home.erb within layouts/application (0.5ms)
Completed 200 OK in 31ms (Views: 30.1ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:21 -0400
Processing by HomeController#home as HTML
Rendered home/home.erb within layouts/application (0.2ms)
Completed 200 OK in 30ms (Views: 29.1ms | ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-10-29 00:51:22 -0400
Processing by HomeController#home as HTML
Rendered home/home.erb within layouts/application (0.6ms)
Completed 200 OK in 96ms (Views: 94.8ms | ActiveRecord: 0.0ms)
Started GET "/articles/new" for 127.0.0.1 at 2015-10-29 00:51:29 -0400
Processing by ArticlesController#new as HTML
Rendered articles/new.erb within layouts/application (1.0ms)
Completed 200 OK in 86ms (Views: 85.2ms | ActiveRecord: 0.0ms)
Unpermitted parameters: title, body
您应该将 article_params
更改为以下
def article_params
params.require(:article).permit(:title, :body)
end
所以,您的服务器日志告诉您问题所在:
Unpermitted parameters: title, body
您必须将 title
和 body
属性列入白名单,因为动作控制器参数在被列入白名单之前禁止用于活动模型批量分配。
变化:
def article_params
params.required(:article).permit(:content)
end
收件人:
def article_params
params.require(:article).permit(:title, :body)
end
此外,请查看 Strong Parameters
的官方 Rails 文档更新
我建议您在 routes
文件中使用 RESTful route:
resources :articles
那么,你会有这条路线:
articles GET /articles(.:format) articles#index
然后,在控制器的创建操作中,您可以使用 articles_path
:
def create
@article = Article.new(article_params)
if @article.save
redirect_to articles_path
else
render 'new'
end
end
它在你的日志中说 -
Unpermitted parameters: title, body
将您的 article_params
方法更改为
def article_params
params.require(:article).permit(:title, :body, :content)
end