Form validation Rails 5.1.4

19 views
Skip to first unread message

Ivo Döhler

unread,
Jan 29, 2018, 11:09:03 AM1/29/18
to Ruby on Rails: Talk
Hello,

I've read the rails introduction and followed the examples. It was quite understandable. But the form validation didn't work as expected.

My form begins with

<%= form_with(model: @article) do |form| %>
  <% if @article.errors.any? %>
...

and in the class is the validation rule:

validates :title, length: { minimum: 5 }
...

part of the controller:

def update

@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render :edit
end
end

the problem is:

if the title is too short (for example) I can push the update-button but nothing happens. There is no reload of the page at all, no errors are shown. Is the title is long enough, the submit-button works correctly. Do I change the form_with to (scope: :article, ...) then the errors are shown.

I use Rails 5.1.4 

Thx for a response

Rolandas Barysas

unread,
Jan 29, 2018, 12:11:18 PM1/29/18
to rubyonra...@googlegroups.com
form_with by default generates form which is submitted by an XHR request. This is why it seems that nothing happens, but if you look to the console output, you'll see that form was actually submitted.

You can make form_with to submit by a standard page refresh by using 'local' option:

<%= form_with(model: @article, local: true) do |form| %>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages