2016-02-10 34 views
0

我正在按照Ruby-on-Rails分步教程並使用Ruby 2.0.0。點 點,相同的文件輸入在教程視頻中工作,但 在我的應用程序中產生錯誤消息。Ruby on Rails中的強制散列參數

這裏是代碼(評論控制器):

class CommentsController < ApplicationController 


    def create 
    @task = Task.new(params[:task_id]) 
    @comment = @task.comments.build(comment_params) 

    if @task.save 
     redirect_to @task, notice: 'Comment successfully posted.' 
    else 
     redirect_to @task, alert: 'Comment not posted.' 
    end 

    end 

    private 

    def comment_params 
    params.require(:comment).permit(:name, :email, :body) 
    # {comment : {name:,email:,body:}} 
    end 

end 

錯誤消息試圖創建一個評論時,我得到的是

ArgumentError in CommentsController#create 

When assigning attributes, you must pass a hash as an argument. 

導致錯誤的線@task = Task.new(params[:task_id])

我該如何解決這個問題?任何幫助讚賞。

回答

3

我假設您在此操作中不會創建任何Task,但您應該找到自己的任務併爲其指定新的Comment。所以,而不是new,你應該使用find

@task = Task.find(params[:task_id]) 
+0

確實!現在工作,非常感謝... –