2017-09-11 54 views
1

我得到一個錯誤,當我坐進文章/編輯。的ActiveRecord :: RecordNotFound在ArticlesController#顯示找不到文章與「ID」 =編輯

我收到此錯誤: enter image description here

時,我應該得到這個錯誤:

enter image description here

我的代碼:

articles_controller:

class ArticlesController < ApplicationController 

    def new 
    @article = Article.new 
    end 

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

    def create 
    @article = Article.new(article_params) 
    if @article.save 
     flash[:notice] = "Article was submitted succsefully" 
     redirect_to (@article) 
    else 
     render :new 
    end 
    end 

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

    private 
    def article_params 
    params.require(:article).permit(:title, :description) 
    end 
end 

我edit.html.erb:

<h1>Edit the existing article</h1> 
<% if @article.errors.any? %> 
<h2>The following errors are informing you that if you don't do these then 
your articles will not be edited</h2> 
    <ul> 
    <% @article.errors.full_messages.each do |msg| %> 
     <li> <%= msg %> </li> 
    <% end %> 
    </ul> 
<% end %> 
<%= form_for @article do |f| %> 
    <p> 
    <%= f.label :title %> 
    <%= f.text_field:title %> 
    </p> 
    <p> 
    <%= f.label :description %> 
    <%= f.text_area :description %> 
    </p> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

我new.html.erb:

<h1>Create an article</h1> 
<% if @article.errors.any? %> 
<h2>The following errors are informing you that if you don't do these then 
your articles will not be created</h2> 
    <ul> 
    <% @article.errors.full_messages.each do |msg| %> 
     <li> <%= msg %> </li> 
    <% end %> 
    </ul> 
<% end %> 
<%= form_for @article do |f| %> 
    <p> 
    <%= f.label :title %> 
    <%= f.text_field:title %> 
    </p> 
    <p> 
    <%= f.label :description %> 
    <%= f.text_area :description %> 
    </p> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

我的routes.rb:

resources :articles 
root 'pages#home' 
get 'about', to: 'pages#about' 

向我要更多的文件,如果有幫助

+3

顯示你在routes.rb中有什麼 –

+0

你使用了什麼查詢參數。 資源:您可以通過URL – MisterCal

+0

DANIL斯佩蘭斯基在這裏它是回覆文章 根「頁面#家」 GET「約」,到:「網頁#關於」 –

回答

6

然後這就是錯誤的原因。 'edit'頁面需要一個id參數否則。在您的控制器:

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

它需要params[:id]

所以你需要使用/articles/id/edit(與你的實際ID代替ID)

+0

我不明白 –

+0

想想看,因爲你不應該同時編輯所有的文章。你一次只能編輯一篇文章,所以如果你在數據庫中有一個文章「id = 5」,那麼進入鏈接'/ articles/5/edit'會帶你進入視圖,在那裏你可以編輯特定條款。有意義嗎? – DanneManne

+0

是的,謝謝.. –

2

resources :articles將創建一個edit_article_path幫手,你應該使用鏈接編輯動作:

<%= link_to("Edit article", edit_article_path(@article)) %> 

這將創建正確的路徑與:id段:

 Prefix Verb URI Pattern     Controller#Action 
    articles GET /articles(.:format)   articles#index 
      POST /articles(.:format)   articles#create 
new_article GET /articles/new(.:format)  articles#new 
edit_article GET /articles/:id/edit(.:format) articles#edit 
    article GET /articles/:id(.:format)  articles#show 
      PATCH /articles/:id(.:format)  articles#update 
      PUT /articles/:id(.:format)  articles#update 
      DELETE /articles/:id(.:format)  articles#destroy 
+0

此外,不是重複表單創建一個'articles/_form.html.erb'部分,並用'<%= render partial:'form'%>'將它包含在新的和編輯視圖中。 http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials – max

+0

正確和禮貌的說法是:'以下錯誤阻止了文章被<%= @ article.new_record? ? 「創建」:「更新」%>'。 – max

相關問題