Ruby on Rails Wednesday, June 29, 2011

In my user_sessions_controller:


class UserSessionsController < ApplicationController
  before_filter :require_no_user, :only => [:create, :new]
  before_filter :require_user, :only => :destroy
  
  def new
    @user_session = UserSession.new
    @message = "Hello."
  end
  
  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Login successful!"
      redirect_back_or_default admin_path
    else
      render :action => :new
    end
  end
  
  def destroy
    current_user_session.destroy
    flash[:notice] = "Logout successful!"
    redirect_back_or_default new_user_session_url
  end
end

========
In views/user_sessions/new.html.erb:

<!--user_sessions#new-->
Admin Login
<%= @message %>

========
In spec/views/user_sessions/new.html.erb_spec.rb:

require 'spec_helper'

describe "user_sessions/new.html.erb" do
  
  context "displays the admin login form" do
    it "shows the title" do
      render
      rendered.should have_content("Admin Login") # this is fine
      rendered.should have_content("Hello.") # I added this just to test something simple, but it didn't work
    end
    
    it "shows the form"
    
    it "shows the button"
  end
  
end
========
If I do "rails server" and check out the page in the browser, there is the "Hello." just as I expect.  But the spec test fails to get it.. it /should/ pass:
1) user_sessions/new.html.erb displays the admin login form shows the title
     Failure/Error: rendered.should have_content("Hello.")
       expected there to be content "Hello." in "Admin Login\n\n\n\n"
     # ./spec/views/user_sessions/new.html.erb_spec.rb:9:in `block (3 levels) in <top (required)>'

So why might the <%= %> not be properly showing the @message?

I actually originally had the form code in the view:
<%= form_for @user_session, :url => user_session_path do |f| %>
<%= f.label :login %>
<%= f.text_field :login, :id=>"admin_login" %>

<%= f.label :password %>
<%= f.password_field :password, :id=>"admin_password" %>

<%= f.submit "Login", :id=>"admin_login_button" %>
<% end %>

.. but testing for this yielded
Failure/Error: render
     ActionView::Template::Error:
       undefined method `model_name' for NilClass:Class

So the @user_session variable wasn't being recognized by the spec test.  Similar problem?

Assistance is appreciated.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/7sy1y0ayaSwJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment