Ajax回调来自不同控制器的原始页面

时间:2012-02-08 04:40:00

标签: ruby-on-rails ajax

我正在使用Ajax将数据发布到我正在构建的应用程序的不同控制器上。用户获得正确回答问题的积分。

我的原创观点是: app / views / questions / 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>

我可以在以下位置发布和保存数据: app / controllers / points_controller.rb

 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]出现在当前视图中( .. questions / index.html.erb )。消息在视图/点上闪烁,这不是当前视图。 flash[:success]应出现在 view / questions / index.html.erb 上。 有什么我做错了或不包括?

1 个答案:

答案 0 :(得分:0)

flash[:success]期待重定向向后。

请尝试使用flash.now[:success] = "Data has been saved"http://guides.rubyonrails.org/action_controller_overview.html#flash-now