fullcalendar passing js vars to rails and vice versa

a 夏天 提交于 2019-11-27 16:29:51

regarding "is this line ok" lines:

 eventSources: [{ url: '/users/:user_id/events', }], // IS THIS LINE OKAY?

you are mixing up your js and ruby which is why it isn't working. '/users/:user_id/events' is not a route. '/users/12234/events is a route. Your js does not understand what :user_id is -> you have to actually put a real user id in there.

the one thing I notice is missing from your controller code is anywhere where you instantiate an actual user... do you have current_user? (ie are you using devise for users to login?) if so then you could plausibly use this:

 eventSources: [{ url: '/users/<%= current_user.id %>/events', }], // IS THIS LINE OKAY?

however - I notice that your js file is named "events.js" and is thus not ruby-related at all - in which case the above is also not going to work because there's no ruby in a plain js file.

You will need to set some kind of javascript environment variable in your erb template... that the javascript code can access.

this is getting iffy in my own knowledge but I'd say a horrible, nasty hack would be to do something like:

<script>
   var user_id = <%= current_user.id %>
</script>

I DO NOT RECOMMEND that you actually do this... google a better way - there must be some tutorials on integrating devise into your js in rails. It's here just to show you how your js and ruby must interact in order for the information to be available to your js.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!