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'。您是否点击了索引页上的“显示”链接? – 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