# Displaying form errors and keeping a simple URL

**URL:** <https://www.ruby-forum.com/t/displaying-form-errors-and-keeping-a-simple-url/164597>\
**Category:** Rails\
**Created:** [April 2, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/displaying-form-errors-and-keeping-a-simple-url/164597 "2009-04-02T23:31:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 2, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/displaying-form-errors-and-keeping-a-simple-url/164597/1 "2009-04-02T23:31:07Z")

</div>

Hi -

I’m using Authlogic for user authentication, and I’m trying to set up  
the login screen (I’m having the same problem with the registration and  
other forms, though, but logging in is a nice simple example).

My problem is that the login screen can be accessed through  
[example.com/login](http://example.com/login) just as I’d like it to be, but when there’s a problem  
with the form input, the url changes to [example.com/user\_session](http://example.com/user_session), which  
I really don’t like. If I change the “render :new” in the controller to  
“redirect\_to login\_path” then I’m sent to the correct url, but the  
rails-generated errors don’t appear, and the information that had been  
entered is wiped out, so I don’t want to do that.

I’m using a named url for the login screen:

map.login ‘/login’, :controller =\> ‘user\_sessions’, :action =\> ‘new’  
map.resource :user\_session

My controller looks like:

def new  
@user\_session = UserSession.new  
end

def create  
@user\_session = UserSession.new(params[:user\_session])  
if @user\_session.save  
flash[:notice] = “You are now logged in”  
redirect\_back\_or\_default account\_path  
else  
render :new  
end  
end

And a simple view, for now:

\<% form\_for @user\_session, :url =\> user\_session\_path do |f| %\>  
\<%= f.error\_messages %\>  
\<%= f.label :email %\>  
  
\<%= f.text\_field :email %\>  
  
  
  
\<%= f.label :password %\> - \<%= link\_to “Forgot Your Password?”,  
password\_reset\_path %\>  
  
\<%= f.password\_field :password %\>  
  
  
  
\<%= f.check\_box :remember\_me %\>\<%= f.label :remember\_me %\>  
  
  
  
\<%= f.submit “Login” %\>  
\<% end %\>

There must be a simple solution to this - I’m just learning rails, but  
even so I feel like I’m missing something totally obvious. Can anyone  
make a suggestion?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 4, 2009, 7:51am UTC](https://www.ruby-forum.com/t/displaying-form-errors-and-keeping-a-simple-url/164597/2 "2009-04-04T07:51:23Z")

</div>

Nothing? Really?

Please?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 4, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/displaying-form-errors-and-keeping-a-simple-url/164597/3 "2009-04-04T20:13:40Z")

</div>

Hi Chris,

On Thu, 2009-04-02 at 23:31 +0200, Chris H. wrote:

> Hi -
> 
> My problem is that the login screen can be accessed through  
> [example.com/login](http://example.com/login) just as I’d like it to be, but when there’s a problem  
> with the form input, the url changes to [example.com/user\_session](http://example.com/user_session), which  
> I really don’t like. If I change the “render :new” in the controller to  
> “redirect\_to login\_path” then I’m sent to the correct url, but the  
> rails-generated errors don’t appear, and the information that had been  
> entered is wiped out, so I don’t want to do that.

I’m not sure about the latest versions of RESTful routing but it used to  
be easy enough to pass the params hash along to the redirected\_to  
method.

redirect\_to :action =\> ‘redirected\_to\_method’, :form\_data =\>  
params[:form\_data]

As a first cut I’d try:  
redirect\_to login\_path, :form\_data =\> params[:form\_data]

Not sure about the error messages.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 5, 2009, 8:44am UTC](https://www.ruby-forum.com/t/displaying-form-errors-and-keeping-a-simple-url/164597/4 "2009-04-05T08:44:37Z")

</div>

Thanks, Bill.

I actually stumbled across another technique, which I adapted from some  
code I found in Lovd By Less ([http://lovdbyless.com](http://lovdbyless.com))

Here’s how it works for the new user form:

Routes:

map.connect ‘/join’, :controller =\> ‘users’, :action =\> ‘new’

Form tag:

\<% form\_for @user, :url =\> ‘/join’, :method =\> :post do |f| %\>

Controller

def new  
@user = User.new(params[:user])  
return unless request.post?  
if @user.save  
flash[:notice] = “Account registered!”  
redirect\_back\_or\_default account\_url  
else  
render :action =\> :new  
end  
end

I’ve only played with it a little bit so far, but it seems to do  
everything I need, in less code than the scaffolding I was working with,  
and I haven’t found a downside yet. I kind of like it.

bill walton wrote:

> Hi Chris,
> 
> On Thu, 2009-04-02 at 23:31 +0200, Chris H. wrote:
> 
> > Hi -
> > 
> > My problem is that the login screen can be accessed through  
> > [example.com/login](http://example.com/login) just as I’d like it to be, but when there’s a problem  
> > with the form input, the url changes to [example.com/user\_session](http://example.com/user_session), which  
> > I really don’t like. If I change the “render :new” in the controller to  
> > “redirect\_to login\_path” then I’m sent to the correct url, but the  
> > rails-generated errors don’t appear, and the information that had been  
> > entered is wiped out, so I don’t want to do that.
> 
> I’m not sure about the latest versions of RESTful routing but it used to  
> be easy enough to pass the params hash along to the redirected\_to  
> method.
> 
> redirect\_to :action =\> ‘redirected\_to\_method’, :form\_data =\>  
> params[:form\_data]
> 
> As a first cut I’d try:  
> redirect\_to login\_path, :form\_data =\> params[:form\_data]
> 
> Not sure about the error messages.
> 
> HTH,  
> Bill
