没有ArticlesController#show中的ActiveRecord :: RecordNotFound找不到没有ID的文章 [英] ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article without an ID
本文介绍了没有ArticlesController#show中的ActiveRecord :: RecordNotFound找不到没有ID的文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试向db提交一些数据,但可以,但是当我尝试检索这些数据时,显示没有ID.ils 4.0.1的找不到Article. 我正在使用ruby 2.0.0和ra
I am trying to submit some data to db and its ok but when i am trying to retrieve those data show that Couldn't find Article without an ID.ils 4.0.1. I am using ruby 2.0.0 and 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
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
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 %>
推荐答案
在实际保存文章之前,您要进行重定向.
You are redirecting before actually saving your article.
此:
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
如果您还想添加一些错误处理:
And if you want to add some error handling as well:
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render :new
end
这篇关于没有ArticlesController#show中的ActiveRecord :: RecordNotFound找不到没有ID的文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文