我对rails上的ruby很新...我的问题是从复选框中获取输出。如果
复选框是false
,我想展示每场音乐会,但我总是得到其他条件......
如何将@concerts = Concert.find(:all, :order => "day")
作为标准输出,如果检查check_box,则获取else条件?
#index.html.erb
check_box('dm', false, options = {}, checked_value = "1", unchecked_value = "0")
# controller.rb
def index
if params[:dm] == '0'
@concerts = Concert.find(:all, :order => "day")
else
@concerts = Concert.find(:all, :order => "day", :conditions => {:bool => false})
end
开始GET" /"对于127.0.0.1在2012-08-14 13:21:39 +0200
由ConcertsController处理#index为HTML
[1m [36mConcert Load(0.0ms)[0m [1mSELECT concerts
。* FROM concerts
WHERE concerts
。bool
= 0 ORDER BY day [0m
[1m [35mConcert Load(15.6ms)[0m SELECT concerts
。* FROM concerts
ORDER BY阵容
[1m [36mConcert Load(0.0ms)[0m [1mSELECT DISTINCT day FROM concerts
ORDER BY day [0m
[1m [35mConcert Load(0.0ms)[0m SELECT DISTINCT club_id from concerts
...
在布局/应用程序中呈现的音乐会/ index.html.erb(78.0ms)
在94ms完成200 OK(浏览次数:78.0ms | ActiveRecord:15.6ms)
答案 0 :(得分:0)
试试这个
def index
if params[:dm] == '0'
@concerts = Concert.find(:all, :order => "day")
else
@concerts = Concert.find(:all, :order => "day", :conditions => {:bool => false})
end
控制器收到的复选框值为字符串形式的0或1。此外,它应该是params[:dm]
。