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 :

,515,
<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'로 어떻게 오류가 났는지 아직도 확신 할 수 없다. 색인 페이지에서 '쇼'링크를 클릭 했습니까? – EJ2015

답변

0

첫 번째 오류에서 방문한 URL을 표시하지 않지만 우연히 실수로 http://devurl/article/edit <으로 갔다고 가정하면 경로에 show 경로가 /article/#id으로 표시되어 시도했습니다. 기사의 ID로 "편집"을 사용하면 Couldn't find Article with id="edit"이됩니다. 당신의 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 
  • 편집을 올바른 업데이트 방법을 사용할 수 있습니다. 감사합니다. @t s
+0

또는 다음과 같이 호출 할 수 있습니다.'if @ article.update (article_params)' –

+0

편집 후 문서를 보여 주지만 업데이트 할 때 업데이트하지 않습니다. –

+1

@ts가 맞습니다. 늦었 어. '@ article.update (article_params)'를 사용해야합니다. 원래 게시 한 방식은 기사를 찾고 필드를 변경하지 않고 다시 저장하는 것이 었습니다. 나는 원래의 대답을 편집하여 올바른 방법을 보여 주었다. – BiggeekTX

관련 문제