成功注册后如何显示祝贺模式?

时间:2012-07-13 09:20:57

标签: jquery ruby-on-rails

我正在使用Rails,Devise gem,jQuery验证插件和Bootstrap。

我可以打电话给$('#congrats')。show();当用户提交,但它正在关闭时,因为用户应该在注册后自动重定向。

另外,如果第一次加载页面,我无法调用,是吗?

如何在适当的时候调用show功能?

我的HTML表格

     <form accept-charset="UTF-8" action="/users" class="sign_up" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="W0u6CgL+sA/n/Zecikl67zLqkIPePQz01Vg0u3bi2nc=" /></div>
<div class="field2">

  <label for="user_first_name">First name</label>
  <input id="user_first_name" name="user[first_name]" size="30" type="text" /></div>

<div class="field2"><label for="user_last_name">Last name</label>
  <input id="user_last_name" name="user[last_name]" size="30" type="text" /></div>

<div class="field2"><label for="user_email">Email</label>
  <input id="user_email" name="user[email]" size="30" type="email" value="" /></div>

<div class="field2"><label for="user_password">Password</label>
  <input id="user_password" name="user[password]" size="30" type="password" /></div>

<div class="field2"><label for="user_password_confirmation">Re-Type Password</label>
  <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" />

</div>

<div class="login_submit">
  <input class="btn login" name="commit" type="submit" value="Sign up" />
  </div>
</form>  

2 个答案:

答案 0 :(得分:1)

http://guides.rubyonrails.org/action_controller_overview.html#the-flash ..闪存系统只是为了处理这种情况。闪存中设置的值将仅保留一次重定向。如果用户要刷新页面,则将自动取消设置闪存值,用户将不会再次看到该消息。

  • 您的用户提交表单。
  • 后端的控制器设置闪烁消息表示成功。它会重定向到相应的页面。
  • 重定向页面上的视图代码会读取flash消息并设置一些js变量。
  • 您在该视图上的js代码读取先前设置的变量,并根据值显示您的congrats模式。

我不记得erb语法,但我可以编写一些伪代码:

if flash message["signup_success"] == true, then
   print this text "var showCongrats = true;"
else print this text "var showCongrats = false;"

在你的js代码中,

$(function() {
   if (showCongrats) { // show the modal; 
   }
})

我们只想让erb向页面打印变量声明。

答案 1 :(得分:0)

你能做的是:

  • 在用户提交表单
  • 后将logged_in_successfully会话变量设置为true
  • 在重定向到页面的布局中,检查该变量是否设置为true
    • 如果是这样,请包含理想情况下属于部分的 congratz 模式,否则不执行任何操作。
  • 在重定向到操作时,取消设置变量logged_in_successfully,或将其设置为false