1

I have a collection, I am showing to the user. But I need to I18n.translate the :key into a readable text from my de.yml.

competences:
  key:
    compkey001: "Werbung / 360"
    compkey001subkey002: "Klassische Werbung / ATL"
    compkey002: "Strategie"

f.input :competence_id, label: "Kompetenz", :as => :select,
:collection => option_groups_from_collection_for_select(
@competence_kategories, :competence_unterkats, t(:key, 
scope: 'basic_data.competences'), :id, t(:key, 
scope: 'basic_data.competences'))

This is not working. It is giving me an error, something like: t() is not a method.

The below line is working, but showing the keys that are not usable by the user:

f.input :competence_id, label: "Kompetenz", :as => :select, 
:collection => option_groups_from_collection_for_select( 
@competence_kategories, :competence_unterkats, :key, :id, :key)

Displayed will be:

(bold)compkey001
  compkey001subkey001
(bold)compkey002
... etc.

How can I get the translations being displayed in a grouped manner?

Thanks for help!

Cokiehh
  • 63
  • 1
  • 11
  • what error do you get `I18n.t` using it? – Rajarshi Das Aug 10 '15 at 14:48
  • CompetenceUnterkat Load (0.0ms) SELECT `competences`.* FROM `competences` WHERE `competences`.`type` IN ('CompetenceU nterkat') AND `competences`.`competence_kategorie_key` = 'compkey001' Rendered case_competences/_form.html.haml (31.2ms) Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/smart_listing-1.1.2/app/views/smart_listing/item/_new.js.erb (31.2ms) Rendered case_competences/new.js.erb (62.4ms) Completed 500 Internal Server Error in 234ms (ActiveRecord: 62.4ms) – Cokiehh Aug 10 '15 at 14:56
  • I think it should be `t(:compkey001, scope: 'basic_data.competences.key') ` instead. – Vamsi Krishna Aug 10 '15 at 15:14
  • Sorry, that is not working. "compkey001" is a key in the de.yml, and no attribute of any model. t(... expects an attribute at that place. And that :compkey001 is a fixed value in my situation. – Cokiehh Aug 10 '15 at 15:18
  • `t("basic_data.competences.key.#{key}")` try this then. – Vamsi Krishna Aug 10 '15 at 15:21
  • That is bringing out this error: `ActionView::Template::Error (undefined local variable or method ``key' for #<#:0xa3c5ab0>):` – Cokiehh Aug 10 '15 at 15:25
  • Changing to: `= f.input :competence_id, label: "Kompetenz", :as => :select, :collection => option_groups_from_collection_for_select( @competence_kategories, :competence_unterkats, (t("basic_data.competences.key.#{:key}")), :id, :key)` shows this error: `ActionView::Template::Error (undefined method `Key' for #):' And if I delete the 2nd .key the error is as written in my very first comment-response. – Cokiehh Aug 10 '15 at 15:29
  • The third param of option_groups_from_collection_for_select should be an attribute or method of competence_kategories object (which returns string) and the fifth parameter should be an attribute or method of competence_unterkats object (which returns string). Then this would work – Vamsi Krishna Aug 10 '15 at 15:39
  • Thanks for your extensive help Vamsi. I understand that. But I do not have any attribute to hold that text. I have only the key. And that is directing to my de.yml for german (or en.yml if english). And I really do not know how to get the text from the de.yml file included instead of the key attribute of my model. May be I need to create a new Hash containing all needed values in the controller or model. I was looking for an easy way to get these translations displayed, but there seems not to be a solution or do I need to poste more details? – Cokiehh Aug 10 '15 at 15:44
  • I think you can try writing methods in each model. In competence_kategories model you can write `def list_categories (1..2).each do |i| [t("basic_data.competences.key.compkey00#{i}")] end end` and you can create a similar method to get sub category values alone in competence_unterkats model `def sub_categories(category) (1..2).each do |i| [t("basic_data.competences.key.#{category}.subkey00#{i}")] end end` – Vamsi Krishna Aug 10 '15 at 15:59
  • Okay. But I do have more than two entries. I now added this to my CompetenceKategorie model `def list_competencekategories CompetenceKategorie.each do |cki| [t(ck.key, scope: 'basic_data.competences')] end end ` and this to my CompetenceUnterkat model `def list_competenceunterkat CompetenceUnterkat.each do |cu| [t(cu.key, scope: 'basic_data.competences')] end end ` How can I address these in the _form.html? – Cokiehh Aug 10 '15 at 19:09
  • 1
    Finally I made it !!! The last statement from Vamsi helped me. I have added a virtual field to my model with: ` def name_of_kategorie I18n.t(key, scope: 'basic_data.competences.key') end ' That (also added on the other model for competence_unterkat) generated a new field in my model that I now am able to use in my view for the option_group: 'option_groups_from_collection_for_select( @competence_kategories, :competence_unterkats, :name_of_kategorie, :id, :name_of_unterkat)' Thanks a lot for helping me here. – Cokiehh Aug 13 '15 at 12:47
  • Your comment @Cokiehh should be marked as valid answer, this is exactly what I was looking for and could probably help more people – Raphaël Dec 29 '17 at 13:09

0 Answers0