2011-12-07 42 views
1

我在rails中保存類別。如果保存成功與msg =成功,並希望傳遞消息,它就會起作用。但是,如果包含多個單詞消息,例如msg =類別已成功保存,或者msg ='類別已成功保存類別「,則說明錯誤URI存在錯誤。如何在URL中包含多字字符串redirect_to

下面的代碼工作:

if @category.save! 

    redirect_to "/view_handler?index=1&msg=successfully&url=#{categories_path}" 

    else 
    render :action => "new" 
    end 

但是這不,用單引號或不:

if @category.save! 

    redirect_to "/view_handler?index=1&msg=category saved successfully&url=#{categories_path}" 

    else 
    render :action => "new" 
    end 

這裏是view_handler代碼:

def view_handler 
    index = params[:index].to_i 
    url = params[:url] 
    msg = params[:msg] 
    if index == 0 #backword 
     session[:page_step] -= 1 #step_back 
     url = session[('page' + session[:page_step].to_s).to_sym]  
    else #forward 
     session[:page_step] += 1 
     session[('page' + session[:page_step].to_s).to_sym] = url 
    end 

    #redirect to the page by url 
    if msg.nil?               
     redirect_to url 
    else 
     redirect_to url, :notice => msg 
    end 
    end 

任何關於錯誤的想法?謝謝。

回答

1

您需要使用URI::Escape方法。

所以,你的例子:

redirect_to URI.escape("/view_handler?index=1&msg=category saved successfully&url=#{categories_path}") 
0

我是一個PHP prorammer,也許我錯了,但似乎編碼URL參數的問題。用空格的詞組嘗試類似url_encode(紅寶石中類似的東西)。

0

我可能會這樣做。

redirect_to :controller => <controller_name>, :action => :view_handler, 
    :msg => "category saved successfully", :url => categories_path, :index => 1 
相關問題