简体   繁体   English

活动记录验证问题

[英]Active Record Validation Issue

The Coursera course on Ruby on Rails that I am taking has us test the validations of two pages of our example, a posts page and a comments page. 我正在参加的有关Ruby on Rails的Coursera课程使我们测试了示例的两个页面(帖子页面和评论页面)的验证。 When I open up the example in my browser the validation on the comments page works just as it should, not allowing me to submit a comment without a post_id or a body. 当我在浏览器中打开示例时,评论页面上的验证将按应有的方式工作,不允许我在没有post_id或正文的情况下提交评论。 The problem is when I expect the same result from the posts page, I am allowed to submit anything without receiving an error message, the submission can have no title or body and still be accepted. 问题是,当我期望从帖子页面获得相同的结果时,可以提交任何内容而不会收到错误消息,提交内容可以没有标题或正文,并且仍然可以被接受。

Here are the two segments of code in question: 这是有问题的两段代码:

Comments 评论

class Comment < ActiveRecord::Base
 belongs_to :post
 validates_presence_of :post_id
 validates_presence_of :body
end

Posts 帖子

class Post < ActiveRecord::Base
 has_many :comments, dependent: :destroy
 validates_presence_of :title
 validates_presence_of :body
end

And the lecture from which I am practicing is here, the code starts at the 9 minute mark. 我正在练习的演讲在这里,代码从9分钟开始。 https://class.coursera.org/webapplications-002/lecture/53 https://class.coursera.org/webapplications-002/lecture/53

Since the comments works as it should, I want to know why my posts page is not working the same way. 由于评论可以正常工作,因此我想知道为什么我的帖子页面无法以同样的方式工作。 It does not seem like it is a typo. 看来这不是错字。 Any help would be greatly appreciated. 任何帮助将不胜感激。

You can start by ruling out your model code by opening a new terminal and typing: 您可以通过打开新终端并输入以下命令排除模型代码开始:

rails console

You will now be in independent ruby mode with your app loaded as well. 您现在也将处于独立的红宝石模式,并且您的应用也已加载。 You can now type: 您现在可以输入:

@post = Post.new(:title => 'My post without a body')

Then to try and save to database you type: 然后尝试输入以下内容保存到数据库:

@post.save

You will get a return of true if it's working, and false if it isn't, if it's false type: 如果有效,则返回true;如果无效,则返回false:

@post.errors.full_messages

This should give you an answer as to why it isn't working. 这应该给您一个答案,为什么它不起作用。 If the save does return true, then you know you're all good and we need to look elsewhere. 如果保存确实返回true,则说明您一切都很好,我们需要寻找其他地方。

You may wish to use the new validates directive : 您可能希望使用新的validates指令

#app/models/post.rb
Class Post < ActiveRecord::Base
   validates :title, :body, presence: true
end

In regards to why this would not catch your "errors" that you've included in your form, there are a number of potential issues. 关于为什么这不能捕获您包含在表单中的“错误”,有许多潜在的问题。

-- -

Controller 调节器

The most probable is that you'll not be populating the @active_record_object of your form. 最可能的是,您不会填充表单的@active_record_object

This will be a problem because it won't give you the error messages (as mentioned by anti-fun ), but may also prevent you from being able to send the correct data you need. 这将是一个问题,因为它不会给您错误消息(如anti-fun ),但也可能会阻止您发送所需的正确数据。

Here's what I would do: 这就是我要做的:

#app/controllers/posts_controller.rb
Class PostsController < ApplicationController
   def new
      @post = Post.new #-> sets the ActiveRecord object for your form
   end

   def create
      @post = Post.new post_params
      @post.save
   end

   private

   def post_params
      params.require(:post).permit(:title, :body)
   end
end

#app/views/posts/new.html.erb
<%= form_for @post do |f| %>
   <% if @post.errors.any? %>
      <% @post.errors.full_messages.each do |error| %>
          <%= error %>
      <% end %>
   <% end %>

   <%= f.text_field :title %>
   <%= f.text_field :body %>
   <%= f.submit %>
<% end %>

This should pass the required data to your controller, allowing you to validate it correctly 这会将所需的数据传递到您的控制器,从而使您可以正确地验证它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM