2015-10-19 54 views
0

頁面顯示一篇文章列表,其中每篇文章中都有一個編輯按鈕。 的問題是,當我點擊編輯我得到以下信息:編輯對象方法不起作用

Couldn't find Article with 'id'=#<Article::ActiveRecord_Relation:0xsomeHEX>

.html.erb文件看起來是這樣的:

<% @articles.each do |article| %> 
    <%= article.title %> 
    <%= article.body %> 
    <% link_to "Edit", edit_article_path(@articles) %> 
<% end %> 

我用下面的方法做了如下控制器。

class ArticlesController < ApplicationController 
    include ArticlesHelper 

    def index 
    @articles = Article.all 
    end 

    def new 
    @articles = Article.new 
    end 

    def create 
    @article = Article.new(article_params) 
    @article.save 
    end 

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

    def update 
    @article = Article.find(params[:id]) 
    @article.update(article_params) 
    end 
end 

回答

1

您的edit_article_path是錯誤的,您將整個關係傳遞給它。

<% link_to "Edit", edit_article_path(article) %> 
+0

這個伎倆! – kalpetros