2013-01-12 83 views
0

我不想使用默認使用翻譯提交按鈕

<%= f.submit %> 

,並創造了它的輔助功能,其中還嵌入了一個圖標。輔助函數需要一個標籤放在新創建的按鈕上。

我打電話這樣說:

<%= submit_button("icon-plus", I18n.translate("helpers.submit.create")) %> 

但現在這個文本按鈕上顯示:

%{}模型toevoegen

相反的:

產品類型toevoegen

如果我使用正常的提交按鈕,則出現正確的文本,所以我的yml文件是正確的。我怎樣才能得到正確的文字在助手中使用?

助手代碼:

def submit_button(icon, label) 
    link_to "javascript:void(0)", :class => 'btn btn-primary', :onclick => "$(this).closest('form').submit()" do 
    raw('<div class="') + icon + raw(' icon-white"> ') + label +raw('</div>') 
    end 
end 
+0

你能提供幫助代碼嗎?我懷疑你需要用'#{model}'替換'%{model}'。 –

+0

添加了助手代碼。我已經嘗試將代碼替換爲#{model},但這不起作用,甚至打破了正常提交的標題。 – rept

回答

0

作爲I18n guide說,該translate函數內插在使用第二個參數(散列)的%{}括號傳遞的變量。

你的情況,你需要這樣做是爲了告訴它model

I18n.t("helpers.submit.create", model: "Product type") 

如果你想將任何模型工作的通用選項,你可以看到Rails的本身看的確是來源on GitHub - 這有點像

I18n.t("helpers.submit.create", model: f.object.class.model_name.human) 

順便說一句,你並不需要(也可能不應該)使用raw那裏。你想要實現的內容很容易用內置的幫助器完成:

link_to ... do 
    content_tag :div, label, class: "#{icon} icon-white" 
end 
+0

真是個好回答!謝謝,完全工作。現在要嘗試替換第一部分,以使其更通用。 – rept

+0

設置爲: def create_or_update(object) 「helpers.submit。」 +(object?(object.persisted?:update::create)::submit).to_s end 所以現在我可以調用create_or_update(f.object),它也可以用於更新。 – rept