2013-03-08 33 views
0

你們如何處理這些?當比較兩個人,但使用他們的性別特定代詞? 「Bob喜歡Stacy,他一直渴望她和她的長髮,她不喜歡Bob,他的毛骨悚然的目光......」Ruby/Rails性別代名詞

因此,Bob是數據庫中的實體,Stacy也是如此。 bob = User.find_by_name('Bob')stacy = User.find_by_name('Stacy')bob.gender返回男性,stacy.gender返回女性。

上面的引用也是來自數據庫中的實體。 Match.find(23).body。我想在數據庫中添加一些通用的東西,並且可以填充它,但似乎不起作用。 @p1.first_name likes @p2.first_name. @p1.She has always longed for @p2.her, and @p2.her_p long hair. @p2.She doesn't like @p2.first_name, and @p2.her_p creepy stares...在這裏,我有以女性代名詞爲名的方法,並返回實際。

在控制器中,@dislike = Match.find(23).body@p1 = bob@p2 = stacy對不對?

在視圖中,<%= @dislike %>

這是行不通的。我也看過gsub,並據此剝離。我可以讓它工作。我只是好奇其他人如何處理這種情況,或者我已經非常糾結於此。

+0

我不太明白問題是什麼。如果文本是模板,請將其作爲模板,並使用erb/equivalent。否則,您需要使用當前上下文使用字符串插值進行評估,但這看起來像目前爲止的模板問題。 – 2013-03-08 19:44:23

回答

0
def rend(p1, p2) 
    body = self.body 
    body = body.gsub(/p1.first_name/, p1.first_name) 
    body = body.gsub(/p2.first_name/, p2.first_name) 
    if p1.gender == 'male' 
    body = body.gsub(/p1.she/, 'he') 
    body = body.gsub(/p1.her_p/, 'his') 
    body = body.gsub(/p1.her/, 'him') 
    body = body.gsub(/p1.herself/, 'himself') 
    body = body.gsub(/p1.hers/, 'his') 
    body = body.gsub(/p1.She/, 'He') 
    body = body.gsub(/p1.Her_p/, 'His') 
    body = body.gsub(/p1.Her/, 'Him') 
    body = body.gsub(/p1.Herself/, 'Himself') 
    body = body.gsub(/p1.Hers/, 'His') 
    end 
    if p1.gender == 'female' 
    body = body.gsub(/p1.she/, 'she') 
    body = body.gsub(/p1.her_p/, 'her') 
    body = body.gsub(/p1.her/, 'her') 
    body = body.gsub(/p1.herself/, 'herself') 
    body = body.gsub(/p1.hers/, 'hers') 
    body = body.gsub(/p1.She/, 'She') 
    body = body.gsub(/p1.Her_p/, 'Her') 
    body = body.gsub(/p1.Her/, 'Her') 
    body = body.gsub(/p1.Herself/, 'Herself') 
    body = body.gsub(/p1.Hers/, 'Hers') 
    end 
    if p2.gender == 'male' 
    body = body.gsub(/p2.she/, 'he') 
    body = body.gsub(/p2.her_p/, 'his') 
    body = body.gsub(/p2.her/, 'him') 
    body = body.gsub(/p2.herself/, 'himself') 
    body = body.gsub(/p2.hers/, 'his') 
    body = body.gsub(/p2.She/, 'He') 
    body = body.gsub(/p2.Her_p/, 'His') 
    body = body.gsub(/p2.Her/, 'Him') 
    body = body.gsub(/p2.Herself/, 'Himself') 
    body = body.gsub(/p2.Hers/, 'His') 
    end 
    if p2.gender == 'female' 
    body = body.gsub(/p2.she/, 'she') 
    body = body.gsub(/p2.her_p/, 'her') 
    body = body.gsub(/p2.her/, 'her') 
    body = body.gsub(/p2.herself/, 'herself') 
    body = body.gsub(/p2.hers/, 'hers') 
    body = body.gsub(/p2.She/, 'She') 
    body = body.gsub(/p2.Her_p/, 'Her') 
    body = body.gsub(/p2.Her/, 'Her') 
    body = body.gsub(/p2.Herself/, 'Herself') 
    body = body.gsub(/p2.Hers/, 'Hers') 
    end 

    body 
end 
+0

用這種方法...很好地工作。 – Dudo 2013-03-08 20:46:56

0

如果您正在使用一個數據庫只是一個列添加到名稱表的性別,如果你使用一個布爾字段,那麼你可以做..

name_a = some_db_query_for_name 
if name_a.gender = true: 
    a = "she" 
else: 
    a = "he" 
在模板

然後只是參考

@name_a likes @name_b, @a is ... 

對不起,它不是紅寶石/ rails代碼,但我看到了問題,這似乎是最好的方法來處理它。