0

記事を表示すると、記事を編集するときに記事を更新するときにエラーが表示されます。それが十分ではないと思うなら、私はあなたにイメージと私のコードを示します。ショーのエラーの記事を表示して編集する際にエラーが発生する

画像:編集エラーの

画像:

私のコード:

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 %> 
    <%= link_to "Back To The List Of Articles", articles_path %> 

ショー。 html.erb:

<h1>Showing selected articles</h1> 

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


     </p> 
     <p> 
      Description: <%= @article.description %> 


    </p> 

    <%= link_to "Back To The List Of Articles", articles_path %> 
    <%= link_to "Edit This Article", edit_articles_path(@article) %> 

articles_controller:

class ArticlesController < ApplicationController 
    def index 

     @articles = Article.all 

    end 
     def new 
     @article = Article.new 
     end 
    def edit 
     @article = Article.find(params[:id]) 
    end 
    def update 
      @article = Article.find(params[:id]) 
      if @article.update 
       flash[:notice] = "article was updated" 
       redirect_to(@article) 
      else 
       render 'edit' 
      end 
    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 

index.html.erb

<h1>ARTICLES</h1> 

     <table> 

     <tr> 

      <th>Title</th> 
      <th>Description</th> 


     </tr> 
     </table> 

      <% @articles.each do |article| %> 

    <td><%= article.title %></td> 
    <td><%= article.description %></td> 
    <td><%= link_to 'Edit', edit_article_path(article) %></td> 
    <td><%= link_to 'Show', article_path(article) %></td> 
    <% end %> 
    <%= link_to "Back To The List Of Articles", articles_path %> 

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 %> 
    <%= link_to "Back To The List Of Articles", articles_path %> 

routes.rbを:

Rails.application.routes.draw do 
    # The priority is based upon order of creation: first created -> highest 
priority. 
    # See how all your routes lay out with "rake routes". 

    # You can have the root of your site routed with "root" 
    # root 'welcome#index' 
resources :articles 

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


    # Example of regular route: 
    # get 'products/:id' => 'catalog#view' 

    # Example of named route that can be invoked with purchase_url(id: 
product.id) 
    # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase 

    # Example resource route (maps HTTP verbs to controller actions 
automatically): 
    # resources :products 

    # Example resource route with options: 
    # resources :products do 
    #  member do 
    #  get 'short' 
    #  post 'toggle' 
    #  end 
    # 
    #  collection do 
    #  get 'sold' 
    #  end 
    # end 

    # Example resource route with sub-resources: 
    # resources :products do 
    #  resources :comments, :sales 
    #  resource :seller 
    # end 

    # Example resource route with more complex sub-resources: 
    # resources :products do 
    #  resources :comments 
    #  resources :sales do 
    #  get 'recent', on: :collection 
    #  end 
    # end 

    # Example resource route with concerns: 
    # concern :toggleable do 
    #  post 'toggle' 
    # end 
    # resources :posts, concerns: :toggleable 
    # resources :photos, concerns: :toggleable 

    # Example resource route within a namespace: 
    # namespace :admin do 
    #  # Directs /admin/products/* to Admin::ProductsController 
    #  # (app/controllers/admin/products_controller.rb) 
    #  resources :products 
    # end 
end 
+0

ルートを表示できますか?記事を表示するためにクリックしたリンクはどれですか?あなたは完全なバックトレースをそこに投稿できますか? – EJ2015

+0

ええ、私は質問に入れます –

+1

'#show'でどのようにエラーが出るのかまだ分かりません。あなたのインデックスページで 'show'リンクをクリックしましたか? – EJ2015

答えて

0

最初のエラーで表示しているURLは表示されませんが、誤ってhttp://devurl/article/edit <に行ったと仮定した場合、showのルートには/article/#idと表示されます記事のIDとして「編集」を使用すると、Couldn't find Article with id="edit"となります。あなたの2番目のエラーで

は、あなたのupdate方法では、あなたは@article.updateを呼んでいるが、ActiveRecord update methodは、明示的idを渡すと、更新するパラメータが必要です。これを試してみてください。..

def update 
@article = Article.find(params[:id]) 
     if if @article.update(article_params) 
      flash[:notice] = "article was updated" 
      redirect_to(@article) 
     else 
      render 'edit' 
     end 
end 
  • 正しいupdateメソッドを使用するように編集。ありがとうございます。
+0

また、 'if @ article.update(article_params)' –

+0

のように呼び出すこともできますが、編集後に記事を表示していますが、更新時には –

+1

@tsが正しいです。遅かった。 '@ article.update(article_params)'を使うべきです。私が最初に投稿したのは、記事を見つけてフィールドを変更せずに再保存することだけでした。元の答えを編集して正しい方法を示しました – BiggeekTX

関連する問題