0

我正在尝试使用特定的控制器操作将用户重定向到注册页面,但它不起作用。

在我的 users_controller.rb 中,我有:

def pre_approved_with_code_action
  @code = params[:code]
  if params[:commit] = 'Cancel'
    redirect_to root_url
    return
  end
  if params[:commit] = 'Create Account'
    flash[:notice] = 'Please register as a new user'
    redirect_to new_user_registration_path(:discount_code => @code)
    return
  end
  if params[:commit] = 'Login And Retry'
    redirect_to new_user_registration_path(:notice => 'Please register as a new user', :discount_code => @code)
    return
  end
end

日志文件说:

Started POST "/pre_approved_with_code_action" for 127.0.0.1 at 2013-06-20 10:05:43 -0700

Processing by UsersController#pre_approved_with_code_action as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"sWENK4ZhisSKHQswUZkDcq8ZxsXU+ym9wQqnYwgPIo4=", "code"=>"SPK_2014", "commit"=>"Create Account"}

Redirected to http://localhost:3000/

因此,它重定向到 root_url,而不是 new_user_registration_path。有任何想法吗?

4

2 回答 2

2

简单的错误,你需要2个等号

而不是if params[:commit] = 'Cancel'应该if params[:commit] == 'Cancel'

于 2013-06-20T17:20:07.447 回答
1
def pre_approved_with_code_action
  @code = params[:code]
  if params[:commit] == 'Cancel'
    redirect_to root_url
    return
  end
  if params[:commit] == 'Create Account'
    flash[:notice] = 'Please register as a new user'
    redirect_to new_user_registration_path(:discount_code => @code)
    return
  end
  if params[:commit] == 'Login And Retry'
    redirect_to new_user_registration_path(:notice => 'Please register as a new user', :discount_code => @code)
    return
  end
end

条件应该有===如果右侧是非空值,则始终计算为真

于 2013-06-20T17:19:36.733 回答