简体   繁体   English

Rails 4,控制器未接收到参数

[英]Rails 4, params not received by controller

I'm trying to setup a post editing page, with links to edit and delete posts. 我正在尝试设置一个帖子编辑页面,其中包含用于编辑和删除帖子的链接。 I'm having troubles because my edit controller somehow doesn't seem to receive the parameters I'm sending. 我遇到了麻烦,因为我的编辑控制器似乎无法收到我发送的参数。 I've printed them out on the page to make sure they're there, and it displays correctly, but when I click the editlink I get ActiveRecord::RecordNotFound Couldn't find Article with 'id'= . 我已经将它们打印在页面上以确保它们在那里,并且正确显示,但是当我单击Editlink时,我得到ActiveRecord::RecordNotFound Couldn't find Article with 'id'=

here's the view with the link: (article.id display correctly on this one) 这是带有链接的视图:(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>

articles controller:(the ... is where I edited more code out as it's irrelevant to this question) articles controller:(...是我在此处编辑更多代码的地方,因为它与该问题无关)

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

routes: 路线:

  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 GitHub链接

Clicking on a link instigates a GET request. 单击链接会引发GET请求。 I'm guessing the update action on your controller will be expecting the params to be a POST request. 我猜您控制器上的更新操作将期望参数是POST请求。

The edit action normally handles GET. 编辑操作通常处理GET。 The update action receives the POST request. 更新操作收到POST请求。

I was trying to find the DHH 2007 Keynote at Railsconf that covers this really well. 我试图在Railsconf上找到DHH 2007主题演讲,它很好地涵盖了这一点。 If you can find it, watch it. 如果可以找到它,请观看它。

See CRUD Verbs and Actions at Rails Guides 在Rails Guides上查看CRUD动词和动作

UPDATE: Just seen your routes file. 更新:刚刚看到您的路线文件。 If it's a GET request then just put the URL directly into the browser. 如果是GET请求,则只需将URL直接放入浏览器即可。 eg 例如

articles/update?id=123

See if params[:id] == 123 in the controller. 查看控制器中的params [:id] == 123。 It should be. 它应该是。

UPDATE 2 更新2

articles_update_path(id: article.id)

Will generate a params has with an :id key but fundamentally you shouldn't be calling update actions with a GET route. 将生成带有:id键的params,但从根本上讲,您不应该使用GET路由调用更新操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM