ActiveRecord::RecordNotFound 在 ArticlesController#show 找不到没有 ID 的文章

ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article without an ID

我正在尝试向 db 提交一些数据并且没问题,但是当我尝试检索这些数据时显示无法找到没有 ID.ils 4.0.1 的文章。 我正在使用 ruby 2.0.0 和 ra

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

结束

the ** contain error. 

class ArticlesController < ApplicationController
  def new
  end
  def create
  @article = Article.new(article_params)
  redirect_to @article
  @article.save
  end

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

   params.require(:article).permit(:title, :full_name, :email, :phone_number, :message)
  end

   end

articles/show.html.erb

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

<p>
 <strong>Full Name:</strong>
 <%= @article.full_name %>
 </p>

 <p>
 <strong>Email:</strong>
  <%= @article.email %>
  </p>

 <p>
  <strong>Phone Number:</strong>
   <%= @article.phone_number %>
  </p>
  <p>
  <strong>Message:</strong>
   <%= @article.message %>
   </p>

articles/new.html.erb

<h1>New Articles</h1>

 <%= form_for :article, url: articles_path do |f| %>
  <p>
   <%= f.label :title %>
   <%= f.text_field :title %>
  <p>
  <%= f.label :full_name %>
  <%= f.text_field :full_name %>
  </p>
  <%= f.label :email %>
   <%= f.text_field :email %>
   <p>
   <%= f.label :phone_number %>
   <%= f.text_field :phone_number %>
   </p>
   <%= f.label :message %>
   <%= f.text_field :message %>
   <p>
    <%= f.submit :send_message %>
    </p>

   <% end %>

您在实际保存文章之前进行了重定向。

这个:

def create
  @article = Article.new(article_params)
  redirect_to @article
  @article.save
end

应该是:

def create
  @article = Article.new(article_params)
  @article.save
  redirect_to @article      
end

如果您还想添加一些错误处理:

def create
  @article = Article.new(article_params)
  if @article.save
    redirect_to @article
  else
    render :new
end