Reputation: 34613
Sorry if this question has been asked before
I've started using a lot of js.erb files in my rails apps and I can't really find any information on the best practices for this sort of thing. Right now I'm cobbling bits an pieces of other people's code
I know when and why to use js.erb, but I'm looking for a total dpsht's quide on how to write js.erb - syntax rules, do's and don't's etc
Upvotes: 2
Views: 7655
Reputation: 65
you shouldn't use .js.erb because it will meet serious problem when user.name is changed. if you want to pass variable to js you can try gem client variable.
Upvotes: 2
Reputation: 899
When I started I found I often used js.erb to transfer data from the server to the client, i.e:
var username = <%= user.name %>;
Using one interpreted language to dynamically write code in another interpreted language felt very wrong, so now I rarely use js.erb. Instead I set the server actions to output data in JSON format, and then use Javascript on the client to download the JSON and parse it into Javascript objects, so code and data stay separate.
Upvotes: 11