2015-10-12 43 views
1

守則控制器從一個動作發送一個變量到另一個Ruby on Rails的

if params["type"] == "user" 
    respond_to do |format| 
    format.html { redirect_to home_user_path, notice: "abc" } 
    end 

如果我發送帶有通知變量則做工精細,但我想用我自己的鍵發送類似

format.html { redirect_to home_user_path, search: "abc" } 

它沒有收到

+0

你想發送一個URL參數嗎?如果是,則執行'redirect_to home_user_path(搜索:「abc」)'' – Santhosh

回答

1

你一定記得你不是「發送」一個變量到另一個動作;你調用另一個動作,並具有可變填充它(數據):


1.實例變量

您可以設置一個實例變量,那麼這將是可用下一個動作:

def your_action 
    @search = "abc" 
    respond_to do |format| 
    format.html { redirect_to home_user_path } #-> @search will be available in the other view 
    end 
end 

2.會話

您目前正在嘗試使用sessions填充數據:

def your_action 
    redirect_to home_user_path, search: "abc" 
end 

#app/views/controller/your_action.html.erb 
<%= flash[:search] %> 

3. URL

最後,你可以通過你的路由設置在URL中的價值:

def your_action 
    redirect_to home_user_path(search: "abc") #-> you'll probably need to set the user object too, judging from the route name 
end 

這條命LD填充一些GET request params路線:url.com/action?param=value


這一切的託換,如前所述,是,你不發送的變量,你會在你的當前控制器動作來初始化它,然後讓下一個動作調用它。

1

這是Ruby的可選花括號的一個問題:有時很難看出哪個方法調用的東西被當作參數來對待。

試試這個:

format.html { redirect_to home_user_path(search: "abc") } 

這將重定向到home_user_path並設置params[:search]爲 「ABC」。

相關問題