getScript jQuery path not working / Ruby on Rails

getscript, jquery, ruby, ruby-on-rails, ruby-on-rails-3

Solution

I believe the execution issue can be solved by moving index.js.erb from

app/assets/javascripts/likes/index.js.erb

to

app/views/likes

This is where Rails looks for templates to render (your script shouldn't be served by the asset pipeline). Rails tackles this through convention - your app automatically routes /likes to the index action.

If you want a more informative route, use the Rails routing guide to generate a new route and match it to the create_likes action in the Likes controller. Then, $.getScript("/create_likes.js") will know where to look

Problem

I've got a jQuery function that is called after a doubleclick on a list item. app/assets/javascripts/tile/tile.js ``` $('#list > li').dblclick(function(){ // styling $(this).toggleClass('liked'); // pass ID to controller var movie_id = $(this).attr("data-id"); $.getScript("/likes.js"); }); ``` Next to applying some new formats to said item my main goal is to make a database entry from my `like` controller. In this Railscast the `index` action from their `comments` controller gets called with this simple line. ``` $.getScript("/comments.js"); ``` Additionally some JavaScript gets called from a `index.js.erb` file. My first problem with understanding the example code from Railscasts is how they define the action. If I wanted to call the action `createLike` from my `likes_controller` how would I call it? Secondly, my attempts so far have all failed because both the JavaScript file doesn't load and the action doesn't get called aswell. Somehow I sense that I've messed up with the paths. Where should I locate the JavaScript files that should get called with the `getScript` function? Files app/assets/javascripts/likes/index.js.erb ``` console.log("Test"); ``` app/controllers/likes_controller.rb ``` class LikesController < ApplicationController protect_from_forgery def index Like.create(:user_id => current_user.id, :item_id => params[:id]) end end ```

Original source