ビルド中のアプリケーションの別のコントローラにデータをポストするためにAjaxを使用しています。ユーザは質問に正しく答えるためのポイントを得る。異なるコントローラからの元のページへのAjaxコールバック
私の元のビューは次のとおりです。アプリ/ビュー/質問/ index.html.erb
<form accept-charset="UTF-8" action="/points/create" method="post" data-remote="true" id="formname">
<div class="field">
<%= radio_button("point", :user_answer, 1) %>
<%= label_tag(:correct_answer, question.answer1) %>
<%= radio_button("point", :user_answer, 2) %>
<%= label_tag(:correct_answer, question.answer2) %>
<%= radio_button("point", :user_answer, 3) %>
<%= label_tag(:correct_answer, question.answer3) %>
<%= radio_button("point", :user_answer, 4) %>
<%= label_tag(:correct_answer, question.answer4) %>
</div>
<div class="actions">
<input type="hidden" id="point_question_correct_answer" name="point[correct_answer]" value="<%= question.correct_answer %>" />
<input type="hidden" id="point_question_id" name="point[question_id]" value="<%= question.id %>" />
<input type="hidden" id="point_current_user" name="point[user_id]" value="<%= current_user.id %>" />
<%= submit_tag "Submit", :class => 'btn btn-primary' %>
</div>
私はデータを投稿して保存することができる午前:アプリ/コントローラ/ points_controller.rb:
[:成功]現在のビューに表示される( ..questions/index.html.erb)def create
@point = Point.new(params[:point])
respond_to do |format|
if @point.save
logger.debug "Data has been saved"
flash[:success] = "Data has been saved"
else
end
end
end
問題は、私はフラッシュたいということです。現在のビューではないビュー/ポイントでメッセージが点滅します。 flash[:success]
は、のview/questions/index.html.erbに表示されます。 私が間違っていることを含んでいるか、含まれていないことがありますか?
フォームビルダーを使用していない特別な理由はありますか? http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for –
@Jeff - 1週間ほど前からレールを学んでいたので、まだフォームに慣れていますビルダー。 – squeezemylime
ああ、つまらない。 flash.nowはあなたのためにそれをしましたか? また、Railsガイドは素晴らしいです:http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object –