未定义的局部变量或方法“post”
undefined local variable or method `post'
这是我的评论表格 _form.html.erb:
<%= form_for([post, @comment]) do |f| %>
<p>
<%= f.text_area :body, placeholder: "Write a comment!" %>
</p>
<br>
<p> <%= f.submit %> </p>
<% end %>
这是我的帖子表格 _form.html.erb:
<%= form_for @post, url: post_path(@post), method: :patch do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "Error") %> Prevent this post from posting</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :Edit_Post %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我的 comments_controller.rb:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
@comment.save
redirect_to @posts
end
def destroy
@user = User.find(session[:user_id])
@posts = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
这是我的 posts_controller.rb:
class PostsController < ApplicationController
before_action :load_post, only: [:show, :edit, :update, :destroy]
def index
@user = User.find(session[:user_id])
@posts = Post.all
end
def welcome
@user = User.find(session[:user_id])
@posts = Post.order("created_at desc").limit(4).offset(1)
@signed_in_user = session[:user_id]
end
def posts
@user = User.find(session[:user_id]) unless session[:user_id] == nil
redirect_to login_path, notice: "You're not logged in" unless @user
@signed_in_user = session[:user_id]
end
def new
@post = Post.new
@user = User.find(session[:user_id])
end
def create
@user = User.find(session[:user_id])
@post = Post.new(post_params)
@post.user_id = @signed_in_user
if @post.save
redirect_to dashboard_path
else
render 'new'
end
end
def show
@user = User.find(session[:user_id])
@signed_in_user = session[:user_id]
end
def edit
@user = User.find(session[:user_id])
end
def update
if @post.update(post_params)
redirect_to @post, notice: "Your post has been updated!"
end
end
def destroy
@user = User.find(session[:user_id])
@post.user_id = @signed_in_user
@post.destroy
redirect_to posts_path
end
private
def load_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:body)
end
end
有这个问题。任何帮助将不胜感激。这是我遇到的错误的图片:
编辑:这是我的日志
Started GET "/posts/24" for 127.0.0.1 at 2015-09-28 18:46:44 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"24"}
[1m[36mPost Load (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", 24]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", "24"]]
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 24]]
Rendered comments/_form.html.erb (1.0ms)
Rendered posts/show.html.erb within layouts/application (7.0ms)
Rendered layouts/_nav.html.erb (1.0ms)
Completed 200 OK in 451ms (Views: 412.0ms | ActiveRecord: 1.0ms)
Rails 可以将实例变量从控制器传递给视图,您应该使用 @post
替换 conmments/_form.html.erb
.
中的 post
<%= form_for([@post, @comment = @post.comments.build]) do |f| %>
undefined local variable or method `post'
你的错误信息说明了一切。它告诉您,您没有在视图的相应控制器操作中定义 post
。这就是您收到该错误的原因。
所以,你需要在相应的控制器动作中定义@post
(通常你在这种情况下使用实例变量)即PostsController
的show
动作。您实际上需要定义 @comment
并且目前您的控制器操作中没有它。因此,像这样更新您的 show
方法:
def show
@user = User.find(session[:user_id])
@signed_in_user = session[:user_id]
# you need to define @post and @comment
@post = Post.find params[:id]
@comment = Comment.new(post: @post)
end
然后,您可以在视图中使用 @post
和 @comment
,因此您的 comments/_form.html.erb
将如下所示:
<%= form_for([@post, @comment]) do |f| %>
. . .
. . .
这是我的评论表格 _form.html.erb:
<%= form_for([post, @comment]) do |f| %>
<p>
<%= f.text_area :body, placeholder: "Write a comment!" %>
</p>
<br>
<p> <%= f.submit %> </p>
<% end %>
这是我的帖子表格 _form.html.erb:
<%= form_for @post, url: post_path(@post), method: :patch do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "Error") %> Prevent this post from posting</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :Edit_Post %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我的 comments_controller.rb:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
@comment.save
redirect_to @posts
end
def destroy
@user = User.find(session[:user_id])
@posts = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
这是我的 posts_controller.rb:
class PostsController < ApplicationController
before_action :load_post, only: [:show, :edit, :update, :destroy]
def index
@user = User.find(session[:user_id])
@posts = Post.all
end
def welcome
@user = User.find(session[:user_id])
@posts = Post.order("created_at desc").limit(4).offset(1)
@signed_in_user = session[:user_id]
end
def posts
@user = User.find(session[:user_id]) unless session[:user_id] == nil
redirect_to login_path, notice: "You're not logged in" unless @user
@signed_in_user = session[:user_id]
end
def new
@post = Post.new
@user = User.find(session[:user_id])
end
def create
@user = User.find(session[:user_id])
@post = Post.new(post_params)
@post.user_id = @signed_in_user
if @post.save
redirect_to dashboard_path
else
render 'new'
end
end
def show
@user = User.find(session[:user_id])
@signed_in_user = session[:user_id]
end
def edit
@user = User.find(session[:user_id])
end
def update
if @post.update(post_params)
redirect_to @post, notice: "Your post has been updated!"
end
end
def destroy
@user = User.find(session[:user_id])
@post.user_id = @signed_in_user
@post.destroy
redirect_to posts_path
end
private
def load_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:body)
end
end
有这个问题。任何帮助将不胜感激。这是我遇到的错误的图片:
编辑:这是我的日志
Started GET "/posts/24" for 127.0.0.1 at 2015-09-28 18:46:44 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"24"}
[1m[36mPost Load (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", 24]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", "24"]]
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 24]]
Rendered comments/_form.html.erb (1.0ms)
Rendered posts/show.html.erb within layouts/application (7.0ms)
Rendered layouts/_nav.html.erb (1.0ms)
Completed 200 OK in 451ms (Views: 412.0ms | ActiveRecord: 1.0ms)
Rails 可以将实例变量从控制器传递给视图,您应该使用 @post
替换 conmments/_form.html.erb
.
post
<%= form_for([@post, @comment = @post.comments.build]) do |f| %>
undefined local variable or method `post'
你的错误信息说明了一切。它告诉您,您没有在视图的相应控制器操作中定义 post
。这就是您收到该错误的原因。
所以,你需要在相应的控制器动作中定义@post
(通常你在这种情况下使用实例变量)即PostsController
的show
动作。您实际上需要定义 @comment
并且目前您的控制器操作中没有它。因此,像这样更新您的 show
方法:
def show
@user = User.find(session[:user_id])
@signed_in_user = session[:user_id]
# you need to define @post and @comment
@post = Post.find params[:id]
@comment = Comment.new(post: @post)
end
然后,您可以在视图中使用 @post
和 @comment
,因此您的 comments/_form.html.erb
将如下所示:
<%= form_for([@post, @comment]) do |f| %>
. . .
. . .