进入文章/编辑时出现错误。
当我应该得到这个错误时:
我的代码:
文章 Controller :
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
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
我的 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 %>
我的 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 %>
我的路线.rb:
resources :articles
root 'pages#home'
get 'about', to: 'pages#about'
如果有帮助,请向我询问更多文件
最佳答案
那么这就是错误的原因。否则“编辑”页面需要一个 id 参数。在你的 Controller 中:
def edit
@article = Article.find(params[:id])
end
它需要params[:id]
所以你需要使用/articles/id/edit
(将id替换为你的实际id)
关于ruby-on-rails - ArticlesController 中的 ActiveRecord::RecordNotFound#show 找不到带有 'id' 的文章 =edit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164765/