我正在尝试使用 Twitter Bootstrap 以模式形式处理我的注册过程。我可以让表单提交,模式关闭,一切正常,除了 DOM 没有不引人注目地更新。
用户登录后,导航栏(呈现为部分并在其中包含 ruby)不应再具有“注册”链接,而应获取“退出”链接。
由于我无法render
在管理表单提交的咖啡脚本文件中使用,我该如何重新渲染导航栏以使其反映成功的注册/模态表单提交?
模态形式(HAML):
= simple_form_for @user, remote: true, html: {data: {type: 'html'}, class: 'form-horizontal'} do |f|
%div.modal-header
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} ×
- if @user.errors.any?
#error_explanation
%h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
%ul
- @user.errors.full_messages.each do |msg|
%li= msg
%div.modal-body
=render 'form', f: f
%div.modal-footer
= f.button :submit, name: 'Sign Up', class: "btn-primary", id: "new_user_button"
=link_to "Sign In", signin_path
jquery处理表单提交:
$ ->
$("#new_user_button").click ->
valuesToSubmit = $("#new_user").serialize()
$("#signup_modal").modal "hide"
$.ajax(
url: "/users"
data: valuesToSubmit
type: "POST"
).success (data, status) ->
#update the DOM?
控制器:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
#SignupMailer.new_subscriber(@user).deliver
sign_in @user
format.html { redirect_to root_path, notice: "Thank you for signing up!" }
format.json { render json: @user, status: :created, location: @user }
# format.js {'create'}
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
#format.js
end
end
Navigation Partial (HAML): 需要在表单提交和模态关闭后不显眼地更改这些元素!
%ul.nav.nav-pills.pull-right
-if signed_in?
%li{:class => "#{'active' if current_page?(user_path(current_user))}"}=link_to "Your page", user_path(current_user)
%li=link_to "Sign out", signout_path, :class => "#{'active' if current_page?(root_path)}"
-else
%li= link_to_modal 'Sign Up', new_user_path, :width=> "600", :height =>"400", :remote => true, :id => "new_user_link"
%li=link_to "Sign In", signin_path, :class => "#{'active' if current_page?(root_path)}"