2012-11-27 36 views
0

的信這是我的index.html.erb我從一個字母到z一個字符,我需要找到服務器上只是點擊字母

<% for char in 'A'..'Z' %> 
    <a href="/pacientes?char=<%= char%>"><%= char%></a> 
<% end %> 

這是我的控制器:

if params[:char].nil? 
    @pacientes = Paciente.all 
elsif 
    @pacientes = Paciente.where("apellido1 = ?", @char = params[:char]) 
end 

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

我需要的是當我在字母A的所有人都開始用字母A單擊,在這一刻我纔可以找到他們,如果對URL我把搜索

+0

\t <%在 'A' 炭.. 'Z' %> \t <%= char%> \t <% end %>

gfallasc

+0

那我的索引

\t <%在 'A' 炭.. 'Z' %> \t <%= char%> \t <% end %>

gfallasc

+0

我可以在數據庫中找到我所擁有的,但我需要的是顯示所有以單擊字母開頭的人! – gfallasc

回答

0

你的全部參數可以使用這個:

# in the controller 
if params[:char].nil? 
    @pacientes = Paciente.all 
elsif 
    @pacientes = Paciente.where("apellido1 LIKE ?", "#{params[:char]}%") 
end 

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

一個較短的版本

# in the controller 
@pacientes = Paciente.where("apellido1 LIKE ?", "#{params[:char]}%") 

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

隨着較短的版本,如果PARAMS [:字符]爲零,where子句將返回所有記錄。

+1

您必須在參數中放置'%':''apellido1 LIKE?「 ,「#{params [:char]}%」'。否則,字符將被消毒,您的查詢將變得格格不入。 – jdoe

+0

謝謝,@ jdoe! – MrYoshiji

相關問題