尝试查看文章时出错

error while trying to see articles

进入 articles/new 时出现此错误。

我的代码:

new.html.erb :

<h1>Create an article</h1>
<%if @article.error.any? %>
<ul>
    <% @article.errors.full_messages.each do |msg| %>
    <li> <%= msg %> </li>
</ul>



<%= 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

我的 articles_controller.erb 文件:

class ArticlesController < ApplicationController

   def new
     @article = Article.new 
   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

    private 
    def article_params 
       params.require(:article).permit(:title, :description)
    end
end 

如果有帮助,请向我要任何其他文件

问题应该出在您的 new.html.erb 文件中的 "end" 字词。您缺少 end 周围的 <% %>

请记住,视图中的所有 ruby 代码必须位于 <% %><%= %>.

您的 erb 文件有很多错误

  • if @article.errors.any? 不是 if @article.error.any?
  • 你没有关闭 if 和 each 块,你也错误地关闭了表单块。

检查下面的代码。

<h1>Create an article</h1>
<% if @article.errors.any? %>
  <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 %>