session [:user_id]返回false为什么?回报率

时间:2014-07-23 16:20:46

标签: ruby-on-rails ruby-on-rails-4 sessionid

我正在调试我的parks_controller.rb创建方法,因为我想为当前用户创建park,但current_user方法返回nil。

这是创建方法:

def create
@park = current_user.parks.new(park_params)
respond_to do |format|
  if @park.save
    format.html { redirect_to @park, notice: 'Park was successfully created.' }
    format.json { render :show, status: :created, location: @park }
  else
    format.html { render :new }
    format.json { render json: @park.errors, status: :unprocessable_entity }
  end
end
end

这是current_user方法:

def current_user
if session[:user_id]
  @current_user ||= User.find(session[:user_id])
  puts "There was no problem"
else
  puts "There was a problem"
end
end

打印出来"出现了问题。"这是user_sessions_controller.rb:

def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
  session[:user_id] = user.id
  flash[:success] = I18n.t("thanks_log_in")
  redirect_to parks_path
else
  flash[:error] = I18n.t("login_problem")
  render action: 'new'
end
end

我的所有测试都在通过,除非我尝试将公园保存到当前用户时,在parks_controller_spec.rb中创建POST的测试开始失败并且我完成了。我究竟做错了什么?我应该在哪里看?我已经花了至少10个小时,我绝望了。请帮忙。提前谢谢。

0 个答案:

没有答案