2013-04-15 48 views
1

寧靜的選擇。如果我在瀏覽器中輸入下面的網址,我得到的客戶記錄列表:現在Rails的url中

http://localhost:5000/clients.json 

,我希望能夠通過URL選擇特定的客戶端。是這樣的可能:

http://localhost:5000/clients.json?locname=ys 

這是我clients_controller.rb索引:

def index 
    @clients = Client.all 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @clients } 
    end 
end 

謝謝!

+0

我們可以看到您的clients_controller.rb文件好嗎? – Andrei

+0

我更新了我的問題。 – Reddirt

回答

0

這應該做的工作:

def index 
    @clients = Client.scoped 
    @clients = @clients.where(:locname => params[:locname]) if params[:locname].present? 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @clients } 
    end 
end 
+0

我得到了'未定義的方法',其中'for#' – Reddirt

+0

我試過了,而不是在哪裏 - 但是,然後format.json行給我= undefined方法調用{:locname =>「ys」 }:Hash – Reddirt

+0

這工作= @客戶端= Client.where(:locname => params [:locname])如果參數[:locname] .present? – Reddirt