Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do i print out validation error?

Matt Taylor asked in Rails

So i'm going through building the forum series and whenever i submit the form, it is just re-rendering the new form page. I looked in my server logs and I see that Parameters: {"utf8"=>"✓", "authenticity_token"=>"wMZ+81Viu/9ch0kpqQwij93jbMqL6d2BXTUj6HWsip90ikYj1XVEPTWEE0Z9D7fcFJ4GXhhPdknjWAXIpnrUSQ==", "forum_thread"=>{"subject"=>"adsfas", "forum_posts_attributes"=>{"0"=>{"body"=>"asdfasdf"}}}, "commit"=>"Create Forum thread"}

Is there a way to see the validations error to figure out why this is not saving to the db?

Thanks

Reply

There are several ways to do this and I actually had this same problem where nested validations were not outputting. I stuck this in my controller where I wanted the object validation errors to be logged to the development.log

Rails.logger.info(@object.errors.inspect)

Of course replace @object with your respective instance variable.

Reply

So i put that in the forum_thread controller and tried to make a new thread and here is the output I got. Processing by ForumThreadsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PsBD72jjI/ndjUj1n6+93rigM/f/XgP4iGoOwwFRFIlXSeneTSlkuNP7z9MsshfgRLjELjZN+AEKx28FNw61Aw==", "forum_thread"=>{"subject"=>"this is crazy", "forum_posts_attributes"=>{"0"=>{"body"=>"why is this not saving"}}}, "commit"=>"Create Forum thread"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]]
**#<ActiveModel::Errors:0x007faeaba473b8 @base=#<ForumThread id: nil, user_id: 3, subject: "this is crazy", created_at: nil, updated_at: nil>, @messages={}, @details={}>**
(0.3ms) BEGIN
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
(0.4ms) ROLLBACK
Rendering forum_threads/new.html.erb within layouts/application

From the activemodel::errors, I'm not sure what to get from that on why it is not saving. 

Any help would be greatlly appreciated. 

Thanks
Reply

Just grabbing at straws here. Looking at the forum_thread_params and my server logs.

params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])

and what is coming through from the form.

"forum_posts_attributes"=>{"0"=>{"body"=>"asdfasdfasfsdfasfasfasfsf"}}}

wouldn't we need to select the 0 from the object first then ask for the body?

Reply

do you have your accepts_nested_attributes_for set on the model as well? The 0 is the row in the hash.

Reply

Hey Brain, this is my forum_thread model file.

class ForumThread < ApplicationRecord
  belongs_to :user
  has_many :forum_posts

  accepts_nested_attributes_for :forum_posts
  validates :subject, presence: true
  validates_associated :forum_posts

end
Reply

Using Rails 5. This is what i had to do to solve my issue. I changed my forum_thread model and added :inverse_of => :forum_thread on the has_many :forum_posts

class ForumThread < ApplicationRecord
  belongs_to :user
  has_many :forum_posts, :inverse_of => :forum_thread
  accepts_nested_attributes_for :forum_posts
  validates :subject, presence: true
  validates_associated :forum_posts

end

I found the answer here: http://stackoverflow.com/questions/16782990/rails-how-to-populate-parent-object-id-using-nested-attributes-for-child-obje

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.