0

I'm new to Ruby Rails and trying to follow along in the textbook, but my validation code isn't working. The original project was created by using the scaffold command:

rails generate scaffold Micropost content:text user_id:integer

Then the instructions tell me to add the following code to my Micropost model:

validates :content, length: { maximum: 140 }, presence: true

The validation works in that the form is not submitted if the length is longer than 140, but the problem is that there is no error message or anything. The button just does nothing. There is supposed to be a red error message, according to the textbook.

Any ideas?

3
  • 1
    Please, check that you have following: 1. You verify "valid?", "validate" on the record in the controller; 2. You have error block on the page that renders errors (aka micropost.errors.full_messages) via flash or some other way. Commented Dec 28, 2021 at 4:24
  • I have the same problem in another project that I started. I have this for my create method: '@email_contact = EmailContact.new(email_contact_params) if '@email_contact.valid? '@email_contact.deliver flash[:success] = 'Message sent!' redirect_to contact_path else flash[:error] = 'Error. Message could not be sent.' render :new end And then the :new method is supposed to refresh the page and I have the form display errors via <% '@email_contact.errors.full_messages.each do |msg| %> but it won't work Commented Jan 16, 2022 at 3:19
  • The apostrophe is because StackOverflow won't let me enter '@'; that isn't actually in my code. Commented Jan 16, 2022 at 3:19

2 Answers 2

1

So I actually just discovered the problem a couple days ago. I was looking into this again for a Rails project I'm been working on, and discovered that in Rails 7 the Turbo Gem that is now included by default will only process render statements on the same controller if we add unprocessable_entity as the status.

So the validation was working, but the user couldn't see anything because the rendering was not working.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems that your form does not show validation errors. Check out this answer https://stackoverflow.com/a/7880392/4077816.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.