2015-04-12 23 views
0

我试图设置一个后期编辑页面,其中包含编辑和删除帖子的链接。我有麻烦,因为我的编辑控制器似乎没有收到我发送的参数。我已经将它们打印在页面上以确保它们在那里,并且它可以正确显示,但是当我单击editlink时,我会得到ActiveRecord::RecordNotFoundCouldn't find Article with 'id'=Rails 4,控制器没有收到参数

这里与链接的观点:(正确article.id显示在这一个)

<div id= "articles-index"> 
    <div class = "row"> 
     <% @article.each do |article| %> 
      <div class="row"> 
       <div class = "container">    
       <p><%= link_to article.title, article %> | <%= article.updated_at.strftime("%e. %b %Y, %H:%M") %> | <%= link_to 'edit', articles_update_path(article.id) %> | delete</p> 
       <h2><%= article.id %></h2> 
       </div>       
      </div> 
     <% end %> 
    </div> 
</div> 

文章控制器:(...的是我编出更多的代码,因为它是不相关的这个问题)

class ArticlesController < ApplicationController   
    ... 
    def update 
     @article = Article.find(params[:id]) 
    end    
    def manage 
     @article = current_user.articles 
     @article = current_user.articles.order('id DESC') 
    end 
    ...  
    def article_params 
     params.require(:article).permit(:id, :title, :body, :user_id) 
    end 
end 

路线:

get 'articles/edit' 
    get 'articles/destroy'  
    get 'articles/manage'  
    get 'articles/show'  
    get 'articles/index'  
    get 'articles/new'  
    get 'articles/update'  
    get 'articles/search'  
    get 'articles/create'  
    get 'sessions/destroy'  
    get 'sessions/new'  
    get 'users/new' 

    resources :users 
    resources :sessions 
    resources :articles 

GitHub Link

+0

不是什么愚蠢的像params ['id']是吗? –

+0

我查看文章时使用完全相同的语法,它的工作原理是'def show @article = Article.find(params [:id]) end',ony那里的视图代码是'<%= link_to article.title,文章%>' –

+0

你可以发布你的文章路线吗? – Vucko

回答

0

点击链接发起GET请求。我猜你的控制器上的更新操作会期望params是POST请求。

编辑操作通常会处理GET。 更新操作收到POST请求。

我试图在Railsconf中找到DHH 2007 Keynote,涵盖了这一点。如果你能找到它,看看它。

在Rails的指南见CRUD Verbs and Actions

UPDATE:刚刚看到你的路由文件。如果它是一个GET请求,那么就把URL直接放到浏览器中。例如

articles/update?id=123 

查看控制器中是否有params [:id] == 123。它应该是。

更新2

articles_update_path(id: article.id) 

会生成一个PARAMS有一个:身份证密钥,但你根本不应该调用带有GET路由更新的动作。

+0

我也考虑过这个问题,所以我尝试使用编辑路径和视图,而不是相同的故事。 'ActiveRecord :: RecordNotFound' –

+0

GET请求中的参数在URL中可见。你会看到一个'?'接着是参数名称和值。例如: https://www.google.co.uk/?q=example –

+0

点击链接时,我得到的网址是'http:// localhost:3000/articles/edit.6'。如果我按照你建议的方式手动输入它,'articles/update?id = 123',它会到达正确的页面。如果params [:id] == 123在控制器中,我不太明白你的意思。我在控制器中声明的唯一内容是张贴在文章中(def article_params)。你能指定如何添加这个请吗? –