Brian's Waste of Time

Tue, 15 Feb 2005

Re: Rails for Struts-ters

Got some feedback on my previous post which is probably best reprinted (now that I've gotten permission) verbatim:

Great article on the comparison of Struts and Rails. 
I have a few pointers on how you can make it even better.

1. Rename app/views/layout/scaffold.rb to app/views/layout/application.rb -- 
then its automatically picked up by all controllers.

2. Using model :user is not necessary unless you don't follow the 
User => user.rb model. When you do (and the generators ensure that you do 
pretty much all the time), the User model is automatically included when the 
class is referenced.

3. These are not necessary: helper_attr :return_to, :user_login. All instance 
variables assigned in a controller action is automatically made available to the view.

I'm not sure what the pass_thru is about, but in any case, here's a more idiomatic 
Rails controller for your example:

class LoginController < ApplicationController
  def index
    if @session['user'] = User.authenticate(@params['user_login'])
      logger.info "User logged in: #{@session['user']}"
      redirect_to_path(@params['return_to'])
    else
      @session['return_to'] = @params['return_to']
      redirect_to :controller => 'rsvp',
                  :action => 'select_invitation',
                  :params => { 'name' => @params['user_login'] }
    end
  end

  def pass_thru
    @return_to = @params['return_to']
    render_action 'login'
  end

  def logout
    reset_session
    redirect_to :controller => 'information', :action => 'index'
  end
end
--
David Heinemeier Hansson

Nice when the guy who wwrote the framework is poking at your code ;-)

0 writebacks [/src/java] permanent link