2016-04-20 75 views
0

我有一些簡單的搜索表單的麻煩。當我在形式傳遞頁面上的信息:Rails 4:簡單的搜索表單與錯誤的重定向

/搜索

,按提交按鈕,它收集數據和搜索,但不是重新加載當前頁面它傳遞PARAMS指數:

/權利要求?UTF8 =✓& claim_id = 49 & PHONE_NUMBER =%2B34(44)444444

但是我想在當前頁面顯示搜索結果(沒有任何AJAX)。 如果我手動傳遞這些參數並更改鏈接,它就會工作!

/搜索?UTF8 =✓& claim_id = 49 & PHONE_NUMBER =%2B34(44)444444

routes.rb中

Rails.application.routes.draw do 
    resources :administrators 
    resources :claims 

    root :to => "sessions#landing" 

    ... 

    get 'search' => 'claims#search' 

    post 'claims/new' => 'claims#new' 
    post 'claims' => 'claims#create' 
end 

我的搜索在功能控制器:

def search 
    @claim = Claim.search(params[:claim_id], params[:phone_number]) 
    end 

如何修改我的路線來做到這一點?

回答

0

所以,這裏發生了一些事情。你需要做兩件事情

  1. 確保您的控制器動作可以處理多種類型的請求
  2. 請確保您有2路,1張貼(因爲你沒有使用AJAX)和1個用於獲取

控制器動作

if request.post?o  
    @claim = Claim.search(params[:claim_id], params[:phone_number]) 
else 
    Claim.all (or whatever you want to display when the page is fresh before a search has been done) 
end 

而且你的路線有點凌亂所以讓我們解決這個問題,當我們加職位。

resources :claims do 
    collection do 
    get 'search' 
    post 'search' 
    end 
end