私は数日間同様の質問をしてきましたが、助けになったものは何も見つかりませんでした。かなり正しいことを理解してはいけません。私は、レール5のアプリケーションでモーダルとajaxを使用するために、Michael Hartlのチュートリアルを修正しようとしています。私は送信された電子メールリンクからパスワードをリセットしようとするまでうまくいっています。何か間違っているのか、それともgmail/sendgridのラッパーに関係しているのか分かりません。Railsサーバーは500の内部サーバーエラーを返しますが、依然としてデータベースに投稿します
適切なアクションごとにhtml.erbファイルをjs.erbに置き換えて、モーダルを開き、jqueryを使用して部分的にモーダルボディのフォームをレンダリングします。リセットリンクを除くすべての機能に最適です。しかし、リセットリンクの場合、リンクがHTMLを探していて潜在的な理由で修正できないため、(おそらく間違っている)と思われるedit.js.erbアクションは起動しませんセキュリティ上の問題(そしてまあまあ)。その結果、リセットリンクがクリックされたときにパスワードリセットフォームを含むモーダルを開くことができませんでした。私がこれを回避する唯一の方法は、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