【发布时间】:2017-09-17 21:11:48
【问题描述】:
尽管几天来我一直在研究类似的问题,但我没有发现任何有用的东西。我一定没有完全正确地理解某些事情。我正在尝试修改 Michael Hartl 的教程以在 rails 5 应用程序中使用模式和 ajax。一切都很好,直到我尝试通过发送的电子邮件链接重置密码,我不确定我是否做错了什么,或者它是否与 gmail/sendgrid 包装器有关。
我通过将 html.erb 文件替换为每个适当的操作的 js.erb 来打开模态,然后使用 jquery 通过部分呈现模态正文中的表单。适用于除重置链接之外的所有内容。但是,在重置链接的情况下,它不会触发 edit.js.erb 操作,我假设(可能不正确)是因为链接正在寻找 html 并且由于潜在的原因而无法修改安全问题(这是正确的)。结果是在单击重置链接时无法打开包含密码重置表单的模式。我可以解决这个问题的唯一方法是将我的 jquery 放在一个 edit.html.erb 文件中并将其包装在脚本标签中。
一旦我能够在单击重置链接时打开模式,就会出现表单,适当时会显示表单提交错误,并且新密码数据会按预期发布到数据库中,但是......我从服务器收到 500 错误,并且没有任何内容被重定向。根据标头,请求是使用 ajax 发出的,将接受 json 并且响应标头将 json 显示为内容类型。是的,我是一个新手,所以我可能错过了一些对那里的人来说很明显的伏特加酒,所以提前道歉。任何帮助,将不胜感激。谢谢。
password_resets_controller.rb
def update
respond_to do |format|
if params[:user][:password].empty?
@user.errors.add(:password, "can\'t be empty")
format.json { render json: @user.errors.full_messages, status: :unprocessable_entity }
elsif @user.update_attributes(user_params)
format.json { head :no_content }
format.js
log_in @user
@user.update_attribute(:rest_digest, nil)
flash[:info] = "Your password has been updated."
redirect_to @user
else
format.json { render json: @user.errors.full_messages, status: :unprocessable_entity }
end
end
end
_edit.form.html
<%= form_for(@user, url: password_reset_path(params[:id]), data: { type: :json } , remote: true, id: "pw-reset-form") do |f| %>
<div class="alert info-<%= message_type %>"><%= message %>
</div>
<%= hidden_field_tag :email, @user.email %>
<%= f.label :password %>
<%= f.password_field :password, placeholder: 'Password', class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, placeholder: 'Password', class: 'form-control' %>
<%= f.submit "Update Your Password", class: "btn btn-primary", remote: true %>
<% end %>
edit.html.erb
<script type="text/javascript">
$('#dialog h3').html("<i class='glyphicon glyphicon-plus'></i> Password Reset");
$('.modal-body').html('<%= j render("password_resets/edit_form") %>');
$('#dialog').modal("show");
$('#dialog').on('shown.bs.modal', function () {
$('.first_input').focus()
})
</script>
user_mailer/password_reset.html.erb
<%= link_to "Reset Password", edit_password_reset_url(@user.reset_token, email: @user.email)%>
路线
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
请求标头
Host: xxxxxxxxx.herokuapp.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://xxxxxxxxx.herokuapp.com/password_resets/WZX92Z-z9OI7EhwWxSfbSg/edit?email=xxxxxx%40gmail.com
X-CSRF-Token: PkTwBobw+9x2y83hLS+QSz8AUhthAYDdUCYjXnryDQ5Ecbr9bDgAmFQ57TtOxHx6/iwerZDX1WLdgxhKP09vw==
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
x-requested-with: XMLHttpRequest
Content-Length: 150
Cache-Control: no-cache
响应头
Connection: keep-alive
Content-Length: 46
Content-Type: application/json; charset=utf-8
Date: Fri, 21 Apr 2017 02:11:49 GMT
Server: Cowboy
Strict-Transport-Security: max-age=15552000; includeSubDomains
Via: 1.1 vegur
x-request-id: 7b6cbb63-7d7b-4e14-9612-079633ba014f
x-runtime: 0.187740
【问题讨论】:
标签: javascript html ruby-on-rails ajax error-handling