我遇到了我的form_for问题
我目前遇到了我的表单问题。我遇到了我的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' %>
如果在我的部分需要额外的代码。请问,我会用其他文件编辑/更新问题。
赞赏所有帮助/解释。先谢谢你。
第一个问题:
添加到文章#显示
def show
@comment = Comment.new
end
谢谢。这就是现在在文章展示页面上显示的表单。我还有一个问题,我希望你能帮助我。当我尝试提交表单时,我会在控制台中回滚。可能知道这是为什么?我将comment_params添加到我的控制器中,因为我注意到它丢失了。 –
@StevenOssorio日志说什么? – Ruslan
@Rusian在2017-02-15 16:42:28 -0500 处开始发布“/ comments”为127.0.0.1#作为HTML控制器创建#创建为HTML 参数:{“utf8”=>“✓”,“authenticity_token” =>“+ S9KFMtuwRAUBTJCpXqhShDGrMU9Jyyu9y5kA0StpdB + n4iBvwWgXAXIxp/fMbNxmgEg4tGW3cH6RkHBSIs +/A ==”,“comment”=> {“body”=>“yo”},“commit”=>“创建注释”} 用户负载(0.1ms)用户“。* FROM”users“WHERE”users“。”id“=?限制? [[“id”,11],[“LIMIT”,1]] (0.0ms)开始交易 (0。0ms)回滚事务 重定向到http:// localhost:3000/ 完成302发现在3ms(ActiveRecord:0.2ms) –
我明白了。我将删除第二部分。谢谢你的建议。 –
你在学习任何教程吗? – Hizqeel
@Hizqeel目前不是。有一段时间没有触及Rails,并希望根据我记忆中的内容创建一个基本的博客,并在我遇到的任何问题上搜索*以查找过去的帖子。可能最好我再看一遍教程。 –