我的 form_for 有问题
I'm having an issue with my form_for
我目前 运行 我的表格有问题。
First argument in form cannot contain nil or be empty
在我的 comments/new 中是 _new.html.erb 的一部分。这是文件:
<% form_for @comment do |f| %>
<%= f.label :body %>
<%= f.text_field :body, placeholder: "Write Message here.." %>
<% end %>
我想做的是呈现向 Article#Show 页面添加评论。
我的代码:
评论控制器
class CommentsController < ApplicationController
before_action :find_comment, only: [:show, :edit, :update, :destroy]
def index
@comments = Comment.all
end
def new
@comment = Comment.new
end
def create
@comment = current_user.comments.build(comment_params)
if @comment.save
redirect_to :back
else
redirect_to '/'
end
end
def show
end
def edit
end
def update
if @comment.update(comment_params)
redirect_to '/'
else
redirect_to :back
end
end
def destroy
@comment.destroy
redirect_to "/"
end
private
def find_comment
@comment = Comment.find(params[:id])
end
def comment_params
params.require(:comment).permit(:body)
end
end
文章#Show
<%= render '/comments/new' %>
如果我需要额外的代码。请提问,我会 edit/update 将问题与其他文件一起提交。
所有 help/explanation 表示赞赏。先感谢您。
第一个问题:
添加到文章#Show
def show
@comment = Comment.new
end
我目前 运行 我的表格有问题。
First argument in form cannot contain nil or be empty
在我的 comments/new 中是 _new.html.erb 的一部分。这是文件:
<% form_for @comment do |f| %>
<%= f.label :body %>
<%= f.text_field :body, placeholder: "Write Message here.." %>
<% end %>
我想做的是呈现向 Article#Show 页面添加评论。
我的代码:
评论控制器
class CommentsController < ApplicationController
before_action :find_comment, only: [:show, :edit, :update, :destroy]
def index
@comments = Comment.all
end
def new
@comment = Comment.new
end
def create
@comment = current_user.comments.build(comment_params)
if @comment.save
redirect_to :back
else
redirect_to '/'
end
end
def show
end
def edit
end
def update
if @comment.update(comment_params)
redirect_to '/'
else
redirect_to :back
end
end
def destroy
@comment.destroy
redirect_to "/"
end
private
def find_comment
@comment = Comment.find(params[:id])
end
def comment_params
params.require(:comment).permit(:body)
end
end
文章#Show
<%= render '/comments/new' %>
如果我需要额外的代码。请提问,我会 edit/update 将问题与其他文件一起提交。
所有 help/explanation 表示赞赏。先感谢您。
第一个问题:
添加到文章#Show
def show
@comment = Comment.new
end