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

17 views
Skip to first unread message

Denis K

unread,
Feb 5, 2017, 3:46:46 AM2/5/17
to Ruby on Rails: Talk
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!




Colin Law

unread,
Feb 5, 2017, 4:03:44 AM2/5/17
to Ruby on Rails: Talk
Look in log/development.log to see what is logged when you do the post and you may get some useful information.

Colin
 

nanaya

unread,
Feb 5, 2017, 4:35:00 AM2/5/17
to rubyonra...@googlegroups.com
Hi,

On Sun, Feb 5, 2017, at 06:18, Denis K wrote:
> 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?
>

it renders 'new' but the current route is `post /articles` (no redirect)
so it is correct.

Joe Guerra

unread,
Feb 5, 2017, 6:30:36 AM2/5/17
to rubyonra...@googlegroups.com
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).




--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1486287271.3697408.870741768.77CD3048%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.

Hassan Schroeder

unread,
Feb 5, 2017, 5:01:17 PM2/5/17
to rubyonrails-talk
On Sat, Feb 4, 2017 at 1:18 PM, Denis K <thee...@gmail.com> wrote:

> 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?

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.

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Scott Jacobsen

unread,
Feb 5, 2017, 6:03:24 PM2/5/17
to Ruby on Rails: Talk
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. 

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages