small medium large xlarge

Swiss_knife_pragsmall
05 Mar 2011, 20:16
Vern Martin (5 posts)

I’m converting a rails 2.3.5 app to rails 3.0.x. I’ve made a lot of progress but I’m stuck with this issue: the only file in my app/views/layouts is application.html.erb. All well and good. But when the controller renders a page, it totally ignores my application.html.erb file. I can force it to access it by adding “render :file => ‘layouts/application’” and then I see it. Unfortunately, the <%= yield %> has not affect and all I see is what’s in application.html.erb. I placed a “throw” statement at the beginning of application.html.erb so I know when and when it’s not being accessed. And without the explicit render, it is not being accessed at all.

I have googled and googled this topic to now avail. I’ve tried a lot of things. But so far no luck.

Generic-user-small
05 Mar 2011, 22:29
Steve Castaneda (11 posts)

Can you please post the contents of your config/environments.rb file, please?

Edit: Actually, what view in particular are you trying to render? Post the contents of that controller method as well just in case.

Swiss_knife_pragsmall
06 Mar 2011, 12:33
Vern Martin (5 posts)

Below are some key bits of the code. There’s not much in environments.rb now. Most of it with Rails 3 has moved to application.rb. The way it works is that the ApplicationController has a “before_filter” that forces the users to login which is a method defined in the AccessController class (AccessController#login). That method is designed to make sure that the user has logged in, and to see what access the user has. Users are allowed to login with only a user name if they are going to perform limited functions. The form for the login is access/login.html.erb shown below. The problem is that when Rails lands there, it has only executed access/login.html.erb, not app/views/layouts/application.html.erb. I know this is the case by both looking at the source of the page and adding a “throw” in the application.html.erb file that would be hit if that code is brought in by Rails.

Here is some of the code:

`

#>>>>>>>>>>>>>>>>>>>>>>>>>>>> environment.rb:
# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Pferd::Application.initialize!

#>>>>>>>>>>>>>>>>>>>>>>>>> config/application.rb:

module Pferd
  class Application < Rails::Application
    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]
  end
end

#>>>>>>>>>>>>>>>>>>>> controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
  WHOAMI = ApplicationController.name
  VERSION = '201005111905'
    
  include ApplicationHelper
    
  before_filter :authorize, :except => :login

  attr_reader :debug
  @debug = nil

  def initialize
    @debug = MiscToolsDebug.new(WHOAMI,VERSION,logger)
  end
    
  before_filter :authorize, :except => [:login,:set_password]
  helper :all # include all helpers, all the time
    
  protected
    
  # All controllers inherit this authorization method, and by default the most stringent
  # authorization is required. The hours controller requires only a user name.
  # It has its own authorize routine that overrides this one.
  # # RETURN: true if redirected.
  def authorize
    whoami = 'authorize'
      
    debug.info(whoami,__LINE__,"(session[:user_id],session[:user_name]) = (#{session[:user_id]},#{session[:user_name]})")
      
    verify_full_login # see application_helper.rb
  end 
    
end

#>>>>>>>>>>>>>>>>>>>>>>>>>> controllers/access_controller.rb:

class AccessController < ApplicationController
  WHOAMI = AccessController.name
  VERSION = '20100509070604'
  attr_reader :debug
  @debug = nil

  def initialize()
    @debug = MiscToolsDebug.new(WHOAMI,VERSION,logger)
  end

  ############# LOOKING FOR ACCESS CONTROL? SEE application_controller.rb. See authorize() function in controllers.

  def login
    whoami = 'login'
    session[:user_name] = nil
    session[:user_id  ] = nil

    if request.post?
      debug = MiscToolsDebug.new(WHOAMI,VERSION,logger)

      user = nil
      redirect = nil
      if !params[:password].blank? # Authenticate a user with a password.
        debug.info(whoami,__LINE__,"Authenticating user (#{params[:user_name]})")
        user = User.authenticate(params[:user_name], params[:password])
        if user
          # A full login has both user id and user name.
          session[:user_id  ] = user.id
          session[:user_name] = user.user_name
          debug.info(whoami,__LINE__,"Authenticated user (#{session[:user_id]},#{params[:user_name]})")
          if user.require_password_change
            flash[:notice] = 'Reset password required.'
            redirect = ":controller => 'access',:action => 'set_password'"
          else
            redirect = ":controller => 'users',:action => 'index'"
          end
        else
          debug.info(whoami,__LINE__,"Authentication failed for user (#{params[:user_name]})")
        end
      else # Authenticate a user without a password
        debug.info(whoami,__LINE__,"Finding user (#{params[:user_name]})")
        user = User.find_by_user_name(params[:user_name])
        if user
          # A user name only login has a user name, but not a user id.
          session[:user_name] = user.user_name
          session[:user_id  ] = nil
          redirect = ":controller => 'hours',:action => 'new'"
        else
          debug.info(whoami,__LINE__,"Failed to find user (#{params[:user_name]})")
        end
      end

      # If we found and the user is enabled, then redirect to the appropriate place.
      if user and user.is_enabled
        redirect = "redirect_to(#{redirect})"
        debug.info(whoami,__LINE__,redirect)
        eval redirect
      else
        # If we found a user, but the user is disabled, then don't go anywhere and show that the user is disabled.
        if user and !user.is_enabled
          flash.now[:notice] = "Sorry user (#{params[:user_name]}) is disabled."
        else
          # Did not find or authenticate the user.
          flash.now[:notice] = "Failed to log in user (#{params[:user_name]})."
        end
      end
    end
  end
 end

#>>>>>>>>>>>>>>>>>>>>>>>> access/login.html.erb:

<% user_name_help = "Type your login name. Just hit enter to begin entering volunteer hours." password_help = "A password is required unless you are just entering your volunteer hours." %> <%= form_tag do %> Please Log In
<%= label_tag 'name', 'User Name:', :title => user_name_help %> <%= text_field_tag :user_name, params[:user_name], :title => user_name_help %>
<%= label_tag 'password', 'Password:', :title => password_help %> <%= password_field_tag :password, params[:password], :title => password_help %>

<%= submit_tag "Volunteer Login (No Password)", :title => 'Login without a password to enter your volunteer hours.' %> <%= submit_tag "Full Login", :title => 'Login with a password to do anything else but enter volunteer hours' %>



<% end %>
`
Swiss_knife_pragsmall
06 Mar 2011, 20:49
Vern Martin (5 posts)

I finally tracked down the problem. By creating a new pristine rails project and gradually adding things to it, I discovered the solution. Since I have my own initialize method in my ApplicationController class it was bypassing the usual initialization. All I had to do was add “super” as the first line of my own initialize routine, and all the usual stuff runs including bringing in the application.html.erb file. I had to track this issue down in other controllers too, but that did the trick. I’m now seeing my application.html.erb file brought into my pages. Whew! Glad that’s done.

`
  def initialize
    super
    # ... the rest of my stuff ...
  end
`
Generic-user-small
13 Jan 2012, 10:29
KJW (1 post)

Thanks for documenting this. I had the same problem except that I was overriding initialize in one of my controllers (not ApplicationController but e.g. MessagesController).

Adding a call to ‘super’ fixed the problem of not loading application.html.erb.

You must be logged in to comment