我目前遇到了我的表單問題。我遇到了我的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以查找過去的帖子。可能最好我再看一遍教程。 –