Ruby Instance Vars from external file
I have lots of repeating flash notices all around my rails 3 app and end up typing the same strings every again and again. As well as that I want to remove long strings for flash notices outside of the controllers.
I tried putting them in an external fil开发者_StackOverflowe but I can't get access to the instance vars...
I posted the code here: http://pastebin.mozilla.org/1201689
Does anyone have any ideas?
Really appreciate any help :)
Tasks like these are design for i18n (internationalization). Have a look at section 3 in http://guides.rubyonrails.org/i18n.html This uses a file to store them all, but you can change it based on language. It demonstrates exactly what you are looking for. Even when you are only using 1 language, this will remove duplicate code for frequently used strings.
The other option is to make a module in your app/lib folder that defines your constants.
module FlashNoticeConstants
MY_COMMONLY_USED_FIRST = "test"
MY_COMMONLY_USED_SECOND = "test2"
end
精彩评论