我正在尝试在注册用户时添加电子邮件确认。用户必须填写包含邀请码和电子邮件地址的表格,然后他们会收到一封包含激活链接的电子邮件。
所以我想做的就是拥有它,以便当用户填写表单时,它提交表单并重新加载表单,以便隐藏并显示消息“我们向您发送了一封电子邮件,请检查并单击链接” “而不是重新加载整个页面。
signup.html.erb
这里我添加了remote: true
<%= form_tag(form_action, method: :get, remote: true, id: 'create-form') do %>
users_controller.rb
重定向有效,但 format.js 是未知格式
def new
... code checking params
if @user.user_email_confirmed
render :new
else
respond_to do |format|
format.html { redirect_to signup_path(@user.name, email: @user.email) }
format.js
end
end
new.js.erb
$('#create-form').hide().after('');
$('.login-form').append('<p class="link-sent">We have sent an activation link to the email address you entered - please be sure to check your spam or trash folders. Please click that link to complete account activation.</p>');
不确定我错过了什么,有什么想法吗?
最佳答案
我真的不认为表单在提交时调用的操作是您的new
操作,它可能是 Controller 中的某些create
操作。
因此,当您点击表单的“提交”按钮时,由于您有 remote: true
选项,它会通过 Ajax 发送,并且它调用的操作没有 格式.js
或 js.erb
View ,这就是为什么你会得到未知的格式。
确保提交表单时调用的是哪个 Controller 操作,然后添加 format.js
和正确的 js.erb
View 。
请看一下这个blog post ,可能会有帮助。
关于javascript - format.js 未执行(ajax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35686803/