ruby-on-rails - ArticlesController 中的 ActiveRecord::RecordNotFound#show 找不到带有 'id' 的文章 =edit

标签 ruby-on-rails ruby rails-activerecord

进入文章/编辑时出现错误。

我收到这个错误: enter image description here

当我应该得到这个错误时:

enter image description here

我的代码:

文章 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/

相关文章:

ruby-on-rails - 嵌套形式的 Rails 复选框

ruby-on-rails - 您可以在Rails中为示波器加上别名吗?

ruby-on-rails - 有人可以用通俗易懂的语言向我解释一下 Ruby on Rails 中映射资源和命名路由的处理方式吗?

mysql - 使用 'having' 的 ActiveRecord Relation 的 Rails update_all 不起作用

ruby-on-rails - Net::HTTP.post_form 抛出 EOFError?

ruby-on-rails - 读取跨域 JSON 响应

ruby-on-rails - Ruby 中的纤维有什么意义?

ruby - 如何在 Ruby 中查找数组中的特定字符

arrays - 如何与数组进行比较并计算不匹配的元素 - Ruby

ruby-on-rails - Rails 3.2.x 关联和 attr_accessible