ruby-on-rails - 远程ajax调用时是否可以使用redirect_to?

标签 ruby-on-rails ajax ruby-on-rails-3

现在这个 Controller 中的这个 Action 被远程ajax调用调用了。
仅当复选框[:call] 被选中并提交时,
我想让它重定向到 ......_path 与 flash 消息。

有可能吗?如果可能,怎么做?

Controller

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

查看表单(使用 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而不是 Controller 来编写这段代码。

在 Controller 中:

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

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

<% 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 %>

希望这对你有用。

关于ruby-on-rails - 远程ajax调用时是否可以使用redirect_to?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093889/

相关文章:

javascript - 更改 downloadurl() 以返回 xml

ruby-on-rails-3 - 禁用单个 Controller 的 session 已被弃用

ruby-on-rails - 是否可以为简单类创建表单

ruby-on-rails - 如何在 heroku 上的两个不同应用程序之间共享工作人员?

ruby-on-rails - 为什么 Ruby on Rails 没有内置标准的用户认证系统?

ruby-on-rails - rails 3路线: Default format and also limited formats

mysql - MySQL 和 PostgreSQL 之间的不同结果

ruby-on-rails - 如何使用 Facebook OAuth 和 Rails Devise gem 在 iOS native 应用程序中创建 session ?

jquery - 使用 jQuery/ajax 选择表格行

javascript - 如何在 WordPress 中实现 AJAX 评论