远程ajax调用时可以使用redirect_to吗? [英] Is it possible to use redirect_to when it's called by remote ajax?

查看:55
本文介绍了远程ajax调用时可以使用redirect_to吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,此控制器中的此操作由远程ajax调用来调用.
仅在选中复选框[:call]并提交后,
我想让它重定向到带有Flash消息的......_ path.

Now this action in this controller is called by remote ajax call.
Only when the checkbox[:call] was checked and it's submit,
I'd like make it to redirect to ......_path with flash message.

有可能吗?如果可能的话,怎么办?

Is it possible? if possible, how?

控制器

.....
if params[:comment][:call] == "1"
    flash[:notice] = "you checked and submit!"
    redirect_to ....._path
else
    ajax page reload
end

查看表单(使用Ajax远程保存)

view Form (remote with ajax)

<%=form_for(([@community, @comment]), :remote => true, :class => 'form' ) do |f| %>
    <%= f.check_box :call, :label => 'call' %> call
        <div class="form-actions">
            <div class="input-append">
                <%= f.text_field :body, :id => "input", :class => "box" %>  
            <button type="submit" class="btn">submit</button>
        </div>
    </div>
<% end %>

评论模型

attr_accessible :icon, :call
....
def call
    @call ||= "0"
end

def call=(value)
    @call = value
end

推荐答案

您将不得不在js中而不是控制器中编写此代码.

You will have to write this code in js instead of controller.

在控制器中:

def action_name
  ....
  respond_to do |format|
    format.js
  end
end

制作一个action_name.js.erb文件并编写以下代码:

Make a action_name.js.erb file and write this code:

<% if params[:comment][:call] == "1" %>
  <% flash[:notice] = "you checked and submit!" %>
  window.location.href = <%= some_url %>
<% else %>
  $("#some_div_id").html("something")    //ajax page reload
<% end %>

希望这对您有用.

这篇关于远程ajax调用时可以使用redirect_to吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆