2017-02-15 183 views
0

我目前遇到了我的表單問題。我遇到了我的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' %> 

如果在我的部分需要額外的代碼。請問,我會用其他文件編輯/更新問題。

讚賞所有幫助/解釋。先謝謝你。

+0

我明白了。我將刪除第二部分。謝謝你的建議。 –

+0

你在學習任何教程嗎? – Hizqeel

+0

@Hizqeel目前不是。有一段時間沒有觸及Rails,並希望根據我記憶中的內容創建一個基本的博客,並在我遇到的任何問題上搜索StackOverFlow以查找過去的帖子。可能最好我再看一遍教程。 –

回答

0

第一個問題:

添加到文章#顯示

def show 
    @comment = Comment.new 
end 
+0

謝謝。這就是現在在文章展示頁面上顯示的表單。我還有一個問題,我希望你能幫助我。當我嘗試提交表單時,我會在控制檯中回滾。可能知道這是爲什麼?我將comment_params添加到我的控制器中,因爲我注意到它丟失了。 –

+0

@StevenOssorio日誌說什麼? – Ruslan

+0

@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) –