【问题标题】:Rails 4 , database not showing all records, after error validationRails 4,数据库未显示所有记录,错误验证后
【发布时间】:2014-06-17 10:02:32
【问题描述】:

我有一个非常实际的错误,在我自己的项目中,我所做的事情是:你有一篇博文,页面最底部的评论部分。现在,它显示了来自适当数据库的所有 cmets,您当然可以通过输入您的姓名(或昵称)和评论来添加评论。

现在,如果您通过验证,一切都很好,页面会重新加载新评论和新表单。 但是,如果你不通过验证就会发生错误,我就是无法摆脱。

undefined method `each' for nil:NilClass

Extracted source (around line #32):

</tr>
</thead>
<tbody>
<% @mains.each do |main| %>
<tr>
<td><%= main.title %></td>
<td><%= main.body %></td>

Rails.root: /home/mateusz/Pulpit/Aptana3_Workspace/My_webpage
Application Trace | Framework Trace | Full Trace

app/views/mains/learn_webdeb.html.erb:32:in `_app_views_mains_learn_webdeb_html_erb__433863331888888116_31266720'
app/controllers/mains_controller.rb:26:in `block (2 levels) in create'
app/controllers/mains_controller.rb:22:in `create'

验证本身效果很好,我通过删除上面介绍的这一部分进行了尝试。在新的数据库注入未通过验证后,我希望我的应用程序打印所有数据库记录,并在该错误消息下方说明为什么此注入不起作用,并带有新的准备就绪形式。

这里有这个麻烦的文件:

...
<table class="table">
    <thead>
        <tr>
            <th>Name:</th>
            <th>Comment:</th>
        </tr>
    </thead>
    <tbody>
        <% @mains.each do |main| %>
            <tr>
                <td><%= main.title %></td>
                <td><%= main.body %></td>
            </tr>
        <% end %>
    </tbody>
</table>

<%= form_for(@main) do |main| %>

    <div>
        <%= main.label :title %>
        <%= main.text_field :title %>
    </div>
    <div>
        <%= main.label :body %>
        <%= main.text_area :body %>
    </div>
    <div>
        <%= main.submit %>
    </div>

    <% if @main.errors.any? %>
    <div>
        <!-- ><h2><%= pluralize(@main.errors.count, "error") %> prohibited this post from being saved:</h2> -->
        <% @main.errors.full_messages.each do |msg| %>
            <p><%= msg %></p>
        <% end %>
    </div>
<% end %>

<% end %>

相关控制器代码:

class MainsController < ApplicationController
  def learn_webdeb
    @main = Main.new
    @mains = Main.all
  end

  def motivation
  end

  def about
  end

  def books_courses
  end

  def contact
  end

  def create
    @main = Main.new(main_params)

    respond_to do |format|
      if @main.save
        format.html { redirect_to @main }
      else
        format.html { render action: "learn_webdeb" }
      end
    end
  end

  def show
    redirect_to :action => "learn_webdeb" # albo redirec_to learn_webdeb_path
  end

  private

    def main_params
      params.require(:main).permit(:title, :body) # podsumowujac pozwala na zmiane tylko title i body w classie main czyli w modelu.
    end

end

我应该如何更改我的代码才能使其正常工作?

【问题讨论】:

  • 贴出相关控制器代码。
  • mains 表中有记录吗?
  • 是的,我已经在数据库中添加了一些数据,一些 cmets。
  • Main.all 的输出是什么?
  • 一些垃圾:姓名:评论:Felippe Jakcostogram Antonio,fakalto!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! fsafdsafasdf asdfasdfasdf lklk pppp ll poiopi fg fg,如果对你有帮助,这里有 github 链接github.com/BobcatMK/My_webpage

标签: database validation ruby-on-rails-4


【解决方案1】:

好的,我修好了。我在电源控制器中更改了此代码

  def create
    @main = Main.new(main_params)

    respond_to do |format|
      if @main.save
        format.html { redirect_to @main }
      else
        format.html { render action: "learn_webdeb" }
      end
    end
  end

到此代码:

 def create
    @main = Main.new(main_params)

    respond_to do |format|
      if @main.save
        format.html { redirect_to @main }
      else
        format.html { redirect_to learn_webdeb_path }
      end
    end
  end

现在一切正常,在注入无效信息的数据库后,我被重定向到同一页面,缺少指出输入的数据无效的错误消息,但我可以立即解决这个问题。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多