0

I wish to access a variable I am setting in controller in the model. Imagine the following:

app/controllers/events.rb

...

@language = language_from_query_string
@events = Event.all

render "events"
...


app/models/event.rb

...
def title
  self["title_#{<reference_to_current_controller>.language}".to_sym]
end
...

Anyway to do this?

tereško
  • 58,060
  • 25
  • 98
  • 150
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
  • You could add instance variables to your model (Event.language), or work with generators or helpers. I also do not see why this cannot end up in the view, which has access to language. – Felix Oct 15 '14 at 10:15
  • I feel it would make more 'sense' to have this in model than in view. Requesting `event.title` in a view that requires a specific language will return its appropriate language, thought that was a neat approach. – Jikku Jose Oct 15 '14 at 10:43
  • Or pass the language from your controller/view, so in model `def title(lang=:en) ...` – Felix Oct 17 '14 at 08:15
  • Started doing that, and somehow feeling there might be better way to do it. – Jikku Jose Oct 17 '14 at 13:36

0 Answers0