Forum: Ruby on Rails Displaying form errors and keeping a simple URL

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-04-02 23:31
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 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, 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 %><br />
  <%= f.text_field :email %><br />
  <br />
  <%= f.label :password %> - <%= link_to "Forgot Your Password?",
password_reset_path %><br />
  <%= f.password_field :password %><br />
  <br />
  <%= f.check_box :remember_me %><%= f.label :remember_me %><br />
  <br />
  <%= 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?
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-04-04 07:51
Nothing? Really?

Please?
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-04-04 20:13
(Received via mailing list)
Hi Chris,

On Thu, 2009-04-02 at 23:31 +0200, Chris Hanks wrote:
> Hi -
>
> My problem is that the login screen can be accessed through
> 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, 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
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-04-05 08:44
Thanks, Bill.

I actually stumbled across another technique, which I adapted from some
code I found in Lovd By Less (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 Hanks wrote:
>> Hi -
>>
>> My problem is that the login screen can be accessed through
>> 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, 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
This topic is locked and can not be replied to.