我目前遇到了我的表单问题。我遇到了我的form_for问题
First argument in form cannot contain nil or be empty
在我的意见/新,这是一个部分为_new.html.erb。这是文件:
<% form_for @comment do |f| %>
<%= f.label :body %>
<%= f.text_field :body, placeholder: "Write Message here.." %>
<% end %>
我想要做的是渲染添加评论文章#显示页面。
我的代码:
评论控制器
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
文章#显示
<%= render '/comments/new' %>
如果在我的部分需要额外的代码。请问,我会用其他文件编辑/更新问题。
赞赏所有帮助/解释。先谢谢你。
我明白了。我将删除第二部分。谢谢你的建议。 –
你在学习任何教程吗? – Hizqeel
@Hizqeel目前不是。有一段时间没有触及Rails,并希望根据我记忆中的内容创建一个基本的博客,并在我遇到的任何问题上搜索StackOverFlow以查找过去的帖子。可能最好我再看一遍教程。 –