2012-08-28 18 views
0

這是一個代碼a * helper.rb。而不是這3種方法(它們是完美的工作)Ruby on Rails中的助手方法不起作用,因爲我需要

def years_of_birth_select(form) 
    form.select :year_of_birth, (1..31).to_a 
    end 

    def months_of_birth_select(form) 
    form.select :month_of_birth, months 
    end 

    def days_of_birth_select(form) 
    form.select :day_of_birth, years 
    end 

我試着打電話給只有一個方法

def date_of_birth_select(form) 
    form.select :day_of_birth, years 
    form.select :month_of_birth, months 
    form.select :year_of_birth, (1..31).to_a 
end 

,它被稱爲是

= date_of_birth_select f 

而且只一個顯示選擇,:year_of_birth,選擇。

我做錯了什麼,我該怎麼做才能正確呼叫date_of_birth_select

回答

0

顯示在視圖中的表單元素是helper方法的返回值,默認爲最後一個表達式。在第一種情況下,每種方法只有一行,所以form.select ...的結果是的返回值,所以表單select被正確顯示。但是,如果將它們合併到一個方法中,則不會返回前兩行的返回值,因此您只能選擇:year_of_birth

爲了讓所有的人都必須來連接的返回值(字符串)聯繫在一起:

def date_of_birth_select(form) 
    ((form.select :day_of_birth, years) + 
    (form.select :month_of_birth, months) + 
    (form.select :year_of_birth, (1..31).to_a)).html_safe 
end 

html_safe末是告訴它不應該逃避字符串,否則將會做軌道默認。