2017-05-25 86 views
0

試圖找出_comment.html.erb文件中的編輯路徑。嵌套編輯路徑不工作

不斷收到此錯誤:

ActiveRecord::RecordNotFound in CommentsController#edit 
Couldn't find Article with 'id'=#<Comment::ActiveRecord_Associations_CollectionProxy:0x007fcac37359e8> 

不知道如何計算如何寫正確的路徑。

評論控制器

class CommentsController < ApplicationController 
    def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.create(comment_params) 
    redirect_to article_path(@article) 
    end 

    def show 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.find(params[:id]) 
    end 

    def edit 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.find(params[:id]) 
    end 


    def destroy 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.find(params[:id]) 
    @comment.destroy 
    redirect_to article_path(@article) 
    end 

    private 
    def comment_params 
     params.require(:comment).permit(:commenter, :body) 
    end 
end 

_comment.html.erb

<p> 
    <strong>Commenter:</strong> 
    <%= comment.commenter %> 
</p> 

<p> 
    <strong>Comment:</strong> 
    <%= comment.body %> 
</p> 

<p> 
    <%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %> 
</p> 

<p> 
    <%= link_to 'Show', [comment.article, comment] %> 
</p> 

<p> 
    <%= link_to 'Destroy Comment', [comment.article, comment], 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %> 
</p> 

路線

resources :articles do 
    resources :comments 
    end 

怎麼辦我寫出正確的道路?

而且,早期的路我已經是這樣的:

<%= link_to 'Edit', edit_article_comment_path(@article, comment) %> 

但它會變成一個空白的編輯形式,即,沒有一個文本框有任何填寫...因此爲什麼我試過其他路徑。

任何幫助,將不勝感激。

謝謝。

評論_form.html.erb

<%= form_for([@article, @article.comments.build]) do |f| %> 
    <p> 
    <%= f.label :commenter %><br> 
    <%= f.text_field :commenter %> 
    </p> 
    <p> 
    <%= f.label :body %><br> 
    <%= f.text_area :body %> 
    </p> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

文章show.html.erb

<p> 
    <strong>Title:</strong> 
    <%= @article.title %> 
</p> 

<p> 
    <strong>Text:</strong> 
    <%= @article.text %> 
</p> 

<h2>Comments</h2> 
<%= render @article.comments %> 

<h2>Add a comment:</h2> 
<%= render 'comments/form' %> 

<%= link_to 'Edit', edit_article_path(@article) %> | 
<%= link_to 'Back', articles_path %> 
+0

雅,你是第二個例子更準確。你最初的錯誤是說當你期待一個單一的對象時你傳遞了一個對象數組。我可以看到你用來渲染你的部分代碼嗎? – Anthony

+0

@Anthony,抱歉,不完全確定你的意思......我的意思是,我知道你的意思,但是...無論如何,還包括文章的演出文件...這是一個呈現評論...讓我知道你的意思,否則。 – user273072545345

回答

0

有一個在_comment.html.erb問題

<%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %> 

應該

<%= link_to 'Edit', edit_article_comment_path(@article, comment) %> 

But it would turn up a blank edit form, ie, none of the text boxes had anything filled in

如果傳遞正確的價值觀在form_for

<%= form_for [@article, @comment] do |f| %> 

EDIT2

您應該檢查井的問題是要傳遞的新實例每次評論,即使是編輯操作。這就是爲什麼你沒有得到形式的任何值

<%= form_for([@article, @article.comments.build]) do |f| %> 

將其更改爲

<%= form_for([@article, @comment]) do |f| %> 

並確保你在CommentsControllernewedit行動或無論你在哪裏分配@article@comment渲染comments/_form

class CommentsController < ApplicationController 
    def new 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.new 
    end 

    def edit 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.find(params[:id]) 
    end 

    #... 
end 
+0

已經用評論的_form.html.erb更新了我的帖子...我現在就嘗試過你了,並且我得到了這個錯誤:'表單中的第一個參數不能包含零或者是空的參考你的行......有任何想法嗎?謝謝! – user273072545345