ruby-on-rails – RailsTutorial登录flash无法正常工作

我试图让错误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 

最佳答案 它看起来像你的app / controllers / sessions_controller.rb,你在底部声明了第二个创建方法,它只是说:

def create
  render 'new'
end

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

点赞