现在这个 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/