您的代碼:
<%= f.select(:contrat, [["CDI", 1], ["CDD", 2], ["Contrat de Travail Temporaire ou d’intérim", 3], ["Freelance", 4], ["Stage", 5]], {}, {class: "form-control form-two-half"}) %>
這將節省1
到數據庫而不是"CDI"
,所以在你的顯示頁面上你有1
。
如果你想在你的展示頁面上顯示「CDI」,有很多方法可以做到這一點。
第一(不推薦,它可以打破一個乾淨的MVC方式)
添加到這個模型
class Job < ActiveRecord::Base
def contrat_string
## if string data type of contrat, you should quote e.g if contrat == "1"
if contrat == 1
"CDI"
elsif contrat == 2
"CDD"
elsif contrat == 3
"Contrat de Travail Temporaire ou d’intérim"
elsif contrat == 4
"Freelance"
else
"Stage"
end
end
end
,並顯示頁面
Type de contrat: <%= @job.contrat_string %>
上第二
Add方法到您的幫手
module ApplicationHelper
def contrat_to_s(contrat)
## if string data type of contrat, you should quote e.g if contrat == "1"
if contrat == 1
"CDI"
elsif contrat == 2
"CDD"
elsif contrat == 3
"Contrat de Travail Temporaire ou d’intérim"
elsif contrat == 4
"Freelance"
else
"Stage"
end
end
end
,並顯示頁面上
Type de contrat: <%= contrat_to_s(@job.contrat) %>
第三(推薦)
你可以把一個數組的定義/config/locales/your_language.yml
例如,如果你e使用英語en.yml
en:
contrat_strings:
1: CDI
2: CDD
3: Contrat de Travail Temporaire ou d’intérim
4: Freelance
5: Stage
在你的幫手e。摹application_helper.rb
module ApplicationHelper
def contrat_selects
I18n.t(:contrat_strings).map { |key, value| [ value, key ] }
end
def contrat_views(value)
I18n.t(:contrat_strings)[value]
end
end
在顯示頁面
Type de contrat: <%= contrat_views(@job.contrat) %>
在形式
<%= f.select :contrat, contrat_selects %>
注:我已經測試。所有爲我工作。
我真的不明白你的問題。你想要什麼?提交後你想從選中的「CDI」中選擇「1」嗎? CDI是選中的文本而不是選定的值。 – 2014-10-17 05:05:06
我已經解釋了更多看現在的問題 – userails 2014-10-17 11:23:17