Gagan
Gagan

Reputation: 573

Where to put JS file in Rails?

I want to know where to put javascript file in ruby on rails.

I am using jQuery dialogue box in my app. All jQuery and CSS files I put under assets/javascripts and assets/stylesheets.

Now I have some JavaScript code in my view page that is "my_event". Where to put this JavaScript code?

I googled for this and found something that suggested to make a my_event.js file and put this file inside the views/useraccount directory.

Seeking your valuable suggestion.

Controller useraccount.rb

View my_event.html.erb

<script>
    $(document).ready(function() {

        $('#calendar').fullCalendar({

            header : {
                left : 'prev,next today',
                center : 'title',
                right : 'month,agendaWeek,agendaDay'
            },
            // US Holidays
            events : '/useraccount/my_event',
            eventClick : function(event) {
                // alert (JSON.stringify(event,null,4)); return false;
                if (event.description) {
                    var a = '<button onclick="addToMycal(' + event.id + ')">UnJoin</button>'
                    $("#Successmes").empty();
                    $('#event-desc').html(event.description);
                    $('#add-to-my-cal').html(a)
                    $("#dialog-modal").dialog({
                        height : 350,
                        width : 600,
                        modal : true,
                        buttons : {
                            Ok : function() {
                                $(this).dialog("close");
                            }
                        }

                    });
                } else {
                    $('#event-desc').html("<h4>Oops!</h4> No Data Available");
                    $("#dialog-modal").dialog({
                        height : 300,
                        width : 363,
                        modal : true,
                        buttons : {
                            Ok : function() {
                                $(this).dialog("close");
                            }
                        }
                        // alert (event.description);
                    });
                }
            },
        });
    });

</script>

<script>
    function addToMycal(val) {
        $.getJSON("/useraccount/delete_event?d=" + val, function(data) {
            // alert (JSON.stringify(data,null,4)); return false;
            $("#Successmes").empty();
            $("#event-desc").html(data.msg)
            $("#add-to-my-cal").remove();

        });

    }

</script>
<style>
    #calendarbody {
        margin-top: 40px;
        text-align: center;
        font-size: 14px;
        font-family: "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;
    }

    #calendar {
        width: 900px;
        margin: 0 auto;
        background-color: #E7EAF6;
    }

</style>
<div id="calendarbody">
    <div id='calendar'></div>
</div>

<div id="dialog-modal" title=" Event Description">
    <p id="event-desc"></p>
    <div id="add-to-my-cal"></div>
    <p id="Successmes"></p>

</div>

Upvotes: 1

Views: 469

Answers (3)

aydar.media
aydar.media

Reputation: 154

Rails 7 answer:

  1. Go to config/importmap.rb and add this:
    pin_all_from "app/javascript/custom", under: "custom"
  2. Then to app/javascript/application.js file and add this:
    import "custom/main"
  3. Add 'custom' folder in your 'app/javascript/'
  4. In 'app/javascript/custom' directory add your js file 'main.js'
  5. Restart your server

Upvotes: 1

Ketan Ghumatkar
Ketan Ghumatkar

Reputation: 730

Keep it in assets/javascripts folder and cross check for if you have written "require_tree ." That's it.

This will be available on every page...

Upvotes: 4

user946611
user946611

Reputation:

Put it in assets/javascripts folder, and require it in your application.js file. If your application.js file has require_tree ., no need to require your js file again.

Upvotes: 3

Related Questions