2013-04-18 70 views
3

現在,此控制器中的此操作由遠程ajax調用調用。
只有當複選框[:call]被選中並提交時,
我想使它重定向到......_路徑並顯示flash消息。當被遠程ajax調用時是否可以使用redirect_to?

可能嗎?如果可能的話,如何?

控制器

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

Comment模型

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

def call=(value) 
    @call = value 
end 

回答

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

希望這會爲你工作。

+0

非常感謝。這是我想要的完美答案:) – cat

4

不,這是不可能的。

你必須處理JavaScript,這將改變窗口的位置:

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

當然,在你的js視圖,您可以使用能夠使用動態值的網址:

window.location.href = '<%= @a_controller_variable %>'; 
相關問題