【问题标题】:error while trying to see articles尝试查看文章时出错
【发布时间】:2017-09-10 01:34:53
【问题描述】:

我在进入文章/新文章时遇到此错误。

我的代码:

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 

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

【问题讨论】:

    标签: ruby-on-rails cloud9-ide cloud9


    【解决方案1】:

    问题应该是出现在您的new.html.erb 文件中的“结束”字样。您缺少 end 周围的 &lt;% %&gt;

    请记住,视图中的所有 ruby​​ 代码必须在 &lt;% %&gt;&lt;%= %&gt; 内。

    【讨论】:

    • 我没看懂你在说什么,你能不能举个例子更清楚一点
    • 他很接近,但真正的问题是new.html.erb 最底部的单词end 应该是&lt;% end %&gt;
    • 是的@jrabramson,你是对的。我已经更新了答案。
    【解决方案2】:

    您的 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 %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多