PostsController 中的 NoMethodError#create undefined method `body'
NoMethodError in PostsController#create undefined method `body'
每当我尝试保存 post 时,都会出现此错误。
这是我的 new.html.erb
文件。
<div id="page_wrapper">
<h1>New Post</h1>
<%= form_for :post, url: posts_path do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "error") %> prevented this post from saving:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
</p>
<p>
<%= f.label :date %><br>
<%= f.text_field :date %><br>
</p>
<p>
<%= f.label :time %><br>
<%= f.text_field :time %><br>
</p>
<p>
<%= f.label :location %><br>
<%= f.text_field :location %><br>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
这是我的 show.html.erb
文件。
<div id="page_wrapper">
<h1 class="title">
<%= @post.title %>
</h1>
<p class="date">
Submitted <%= time_ago_in_words(@post.created_at) %> Ago
</p>
<p class="time">
<%= @post.time %>
</p>
<p class="location">
<%= @post.location %>
</p>
</div>
这是我的 posts_controller.rb
文件。
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body, :location)
end
end
她的是我的 routes.rb
文件
Rails.application.routes.draw do
resources :posts
root "posts#index"
end
params.require(:post).permit(:title, :body, :location)
您需要 :body
,但在您的表格中您有 title, date, time and location
。
删除它并确保你的强参数中有这样的属性:
params.require(:post).permit(:title, :date, :time, :location)
您已在 post.rb
中添加了 body
的验证。但是您的 posts
table 中没有字段 body
。
因此,当调用 @post.create
时,post.rb
中定义的所有验证都是 运行 并且由于您没有 body
字段,您得到Undefined method 'body' for Post
错误。
要解决它,请在 post.rb
中删除 body
的验证
删除以下行
validates :body, presence: true
你应该可以开始了。
每当我尝试保存 post 时,都会出现此错误。
这是我的 new.html.erb
文件。
<div id="page_wrapper">
<h1>New Post</h1>
<%= form_for :post, url: posts_path do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "error") %> prevented this post from saving:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
</p>
<p>
<%= f.label :date %><br>
<%= f.text_field :date %><br>
</p>
<p>
<%= f.label :time %><br>
<%= f.text_field :time %><br>
</p>
<p>
<%= f.label :location %><br>
<%= f.text_field :location %><br>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
这是我的 show.html.erb
文件。
<div id="page_wrapper">
<h1 class="title">
<%= @post.title %>
</h1>
<p class="date">
Submitted <%= time_ago_in_words(@post.created_at) %> Ago
</p>
<p class="time">
<%= @post.time %>
</p>
<p class="location">
<%= @post.location %>
</p>
</div>
这是我的 posts_controller.rb
文件。
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body, :location)
end
end
她的是我的 routes.rb
文件
Rails.application.routes.draw do
resources :posts
root "posts#index"
end
params.require(:post).permit(:title, :body, :location)
您需要 :body
,但在您的表格中您有 title, date, time and location
。
删除它并确保你的强参数中有这样的属性:
params.require(:post).permit(:title, :date, :time, :location)
您已在 post.rb
中添加了 body
的验证。但是您的 posts
table 中没有字段 body
。
因此,当调用 @post.create
时,post.rb
中定义的所有验证都是 运行 并且由于您没有 body
字段,您得到Undefined method 'body' for Post
错误。
要解决它,请在 post.rb
body
的验证
删除以下行
validates :body, presence: true
你应该可以开始了。