当远程ajax调用redirect_to时是否可以使用?

时间:2013-04-18 22:03:55

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

现在,此控制器中的此操作由远程ajax调用调用 只有当复选框[:call]被检查并且提交时,
我想让它重定向到带有flash消息的...... _路径。

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

控制器

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

2 个答案:

答案 0 :(得分:4)

不,这是不可能的。

你必须渲染将改变窗口位置的javascript:

window.location.href = 'http://your new location';

当然,在您的js视图中,您可以使用以下命令为网址使用动态值:

window.location.href = '<%= @a_controller_variable %>';

答案 1 :(得分:4)

您必须在js中编写此代码而不是控制器。

在控制器中:

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

希望这对你有用。