why does my failed form submission not redirect to the originating form page?

13 views
Skip to first unread message

S Ahmed

unread,
May 8, 2014, 9:13:48 AM5/8/14
to rubyonra...@googlegroups.com
Hi,

I have the following:

GET  /account/create
POST /account/oncreate

My create action looks like:

def create
 @user = User.new
end

def oncreate
  @user = User.new(params)
  if @user.valid?
       ...
   else
       render 'create'
   end
end


But when the form submission submits and fails, the browser url is on /account/oncreate

Why is that?  I want it to continue to display /account/create

Colin Law

unread,
May 8, 2014, 9:40:58 AM5/8/14
to rubyonra...@googlegroups.com
On 8 May 2014 14:13, S Ahmed <sahme...@gmail.com> wrote:
> Hi,
>
> I have the following:
>
> GET /account/create
> POST /account/oncreate
>
> My create action looks like:
>
> def create
> @user = User.new
> end
>
> def oncreate
> @user = User.new(params)
> if @user.valid?
> ...
> else
> render 'create'

That is better as
render action: 'create'
or
render :action => 'create'
but I would have expected an error if it did not work. Are you sure
the validation is failing? Put some debug code after the if line, if
you use puts then it will be printed to the server terminal window.

Also it is normally better to use the standard actions new and create
rather than create and oncreate. If you have not already done so then
work right through a good tutorial such as railstutorial.org (which is
free to use online) which will show you the basics of rails.

Colin

Colin

Matt Jones

unread,
May 9, 2014, 10:08:54 AM5/9/14
to rubyonra...@googlegroups.com
If you're only rendering a different view (as in the `else` branch above), the URL is not going to change to reflect that.

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages