Going through /getting_started.html — stuck on step 5.10 "Validation" and "render 'new'"

Hi,

I’m new to Rails and going through “Getting Started with Rails”

I’ve stuck in step 5.10:

def create

@article = Article.new(article_params)

if @article.save

redirect_to @article

else

render 'new'

end

end

As I understand posting new article with invalid title should invoke else statement and render [/new] with error message

It shows error but throws to [http://localhost:3000/articles], instead of [http://localhost:3000/articles**/new**]

Is it how it supposed to work?

Or what am I doing wrong?

Thank you all in advance!

Look in log/development.log to see what is logged when you do the post and
you may get some useful information.

Colin

Hi,

Take a look at your article.rb file. You have validations in it that prevent you from writing data to your table (unless you meet those conditions).

Yes.

Note the difference between "redirect_to" which sends a redirect
response header and new URL to the user-agent vs. "render" which
stays at the same URL and provides the content of the "new" view.

Yes, this is the default behavior. When you post invalid data it renders rather than redirects. One reason is this way you don’t need to maintain state across the redirect.