【问题标题】:Ajax callback to originating page from different controller来自不同控制器的原始页面的 Ajax 回调
【发布时间】:2012-02-08 04:40:00
【问题描述】:

我正在使用 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 上。 我做错了什么或不包括什么?

【问题讨论】:

  • 您不使用表单生成器的任何特殊原因? api.rubyonrails.org/classes/ActionView/Helpers/…
  • @Jeff - 大约一周前开始真正学习 Rails,所以我还在习惯表单构建器。
  • 啊,明白了。 flash.now 为你做了吗?此外,Rails 指南也很棒:guides.rubyonrails.org/…
  • 当然......其他一切都进行得很顺利,我认为有一些基本的东西我没有得到这个,因为它应该很简单但我正在撞墙..

标签: ruby-on-rails ajax


【解决方案1】:

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

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

【讨论】:

  • 尝试了 flash.now,但无济于事....如果我想不通,我可能会探索以非 Ajaxian 方式执行此操作。
猜你喜欢
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多