Note that there are some explanatory texts on larger screens.

plurals
  1. POGlobal variable for language_id available in views and in models
    primarykey
    data
    text
    <p>I'm trying to share a session variable in both the controllers, the views and the model.</p> <p>With the following code, it is working in the controllers and in the views :</p> <pre><code>class ApplicationController &lt; ActionController::Base protect_from_forgery helper_method :best_language_id # Returns the ID of the language to use to display the description. def best_language_id @best_language_id ||= session[:best_language_id] @best_language_id ||= current_project.default_language.id return @best_language_id end end </code></pre> <p>But I can't call it from the model.</p> <p>I would like to be able to call best_language_id either in the controllers, views and in one model, to get a fallback of the best_language_id if a translation is not found.</p> <p>Example in my model (not working) : </p> <pre><code>class Point &lt; ActiveRecord::Base # Retuns the attached word in the given language if exists. # Otherwise, falls back on another translation def word(preffered_language_id) word = Word.find(:translation_id =&gt; self.translation_id, :language_id =&gt; preffered_language_id) if word.blank? word = translations.where(:translation_id =&gt; self.translation_id, :language_id =&gt; best_language_id) end return word end end </code></pre> <p>I know that model should not include applicationcontroller method calls, but how is it possible to share my best_language_id accross controllers and model ?</p> <p>Edit : using i18n is not the question here. Translations are not fixed string but variables in a database.</p> <p>Thanks for helping !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload