RailsTutorial登录闪存不工作

时间:2011-05-09 23:02:51

标签: ruby-on-rails ruby railstutorial.org

我试图让错误flash出现在railstutorial.org示例应用中。到目前为止,这就是我所做的。任何人都可以帮我找到错误:

我的RSPEC测试失败:

1) SessionsController POST 'create' invalid signin should have a flash.now message
Failure/Error: flash.now[:error].should =~ /invalid/i
expected: /invalid/i
     got: nil (using =~)
./spec/controllers/sessions_controller_spec.rb:27

控制器代码:

def create
  user = User.authenticate(params[:session][:email],
  params[:session][:password])
  if user.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Sign in"
    render 'new'
  else
    # Sign the user in and redirect to the user's show page.
  end
end

测试代码:

describe "POST 'create'" do

describe "invalid signin" do

  before(:each) do
    @attr = { :email => "example@gmail.com", :password => "invalid"}
  end 

  it "should re-render the new page" do
    post :create, :session => @attr
    response.should render_template('new')
  end 
  it "should have a flash.now message" do
    post :create, :session => @attr
    flash.now[:error].should =~ /invalid/i
  end 
end 

1 个答案:

答案 0 :(得分:2)

在您的app/controllers/sessions_controller.rb中,您在底部声明了第二个创建方法,只是说:

def create
  render 'new'
end

第二个声明正在取代文件中较早的创建方法。只需删除这三行(文件中为20-22),你的规格就会以漂亮的颜色传递,我的意思是只有一种颜色,即绿色。