GetScript jQuery path not working / Ruby on Rails

I have a jQuery function called after double-clicking 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"); }); 

Along with applying some new formats to the specified element, my main goal is to record the database from my like controller. In this Railscast, the index action from their comments controller is invoked using this simple line.

 $.getScript("/comments.js"); 

In addition, some JavaScript is called from the index.js.erb file.

My first problem with understanding Railscasts sample code is how they define an action. If I wanted to call the createLike action from my likes_controller , what would I call it?

Secondly, my attempts so far have not worked, because both JavaScript files are not loading, and the action is also not called.

Somehow I feel like I messed up the way. Where should I look for JavaScript files that should be called using the getScript function?

Files

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

 console.log("Test"); 

application / 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 
+7
jquery ruby ruby-on-rails ruby-on-rails-3
source share
5 answers

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

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

to

app / views / likes

Here, Rails looks for templates to render (your script should not be served by an asset pipeline). Rails solves this by convention - your application automatically directs / likes the action of the index.

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

+3
source share

You can define an action in the controller as follows:

 class LikesController < ApplicationController # another code def createLike # your action code end # another code end 

And you can call the action like /likes/createLike .
In the folder PATH_TO_APP/app/views/likes create a file createLike.html.erb - createLike view appears

Javascript files must be located in the /PATH_TO_APP/public/javascripts folder
And the best way to include a javascript file is javascript_include_tag , for example:

 <%= javascript_include_tag "tile/tile.js" %> 

tile.js file must be located in the /PATH_TO_APP/public/javascripts/tile directory.

And if you want to get javascript files with jQuery, you have to put them in the public/javascripts directory and call $.getScript('/javascripts/likes.js'); - there is an example.

PS I advise you to see the getting started guide

+2
source share

The behavior you want is different from what the Railscast recipients specify. It is specifically focused on receiving new comments as they are created without refreshing the page. This is why you encounter problems arising from this guide.

First you need to make sure you have resources :likes in your config / routes.rb. From your code snippet, it looks like you are associating this with a movie, so be sure to make the route nested in your resources :movies call. At the end, your routes should look something like this:

 resources :movies do resources :likes end 

For the controller part, you will need to add the “create” action to your controller. Assuming your movie model has_many :likes , this is a simple version of what your action should look like:

 def create movie = Movie.find(params[:movie_id]) movie.likes.create(user_id: current_user.id) end 

You will also need to modify your javascript code to post a message instead of a receive request. This is because the http method is how Rails distinguishes between a create request and an index, since they both use the same URL path (e.g. / comments.js). You will also need the url to display that it is an embedded resource in the movie. Here is a modified version of your JS code with this change:

 $('#list > li').dblclick(function() { // Cached jquery this selector. $this = $(this) // pass ID to controller var movie_id = $this.data('id'); $.post('/movies/' + movie_id + '/likes.js', function() { $this.toggleClass('liked'); }); }); 

As for your .js.erb file, as indicated by others, it should be placed in your app/views folder. However, due to your usual JS logic handling, you don't need all of this.

This is just one strategy, but there are many other ways to handle JS interaction with Rails. If you need an example of using the js.erb view file (js.coffee in this case), you can take a look at this implementation . In this case, everything that handles the click event is link_to with the remote: true parameter, which the jquery-ujs adapter delegates to it.

Hope this helps!

+2
source share

It may not be so close to your answer, but I use $.getscript() to load the js/css files that I need when my page is displayed, which in turn improves performance and reduces page load time. This is the code I used in my erb files. My shop_for_free_module.js is in app/public/javascripts

 <script type="text/javascript"> jQuery(document).ready(function($){ //this gives your protocol-http var protocol = this.location.protocol; //this gives your domain name-myshopptinsite.com var host = this.location.host; var initial_url = protocol + "//" + host; $.getScript(initial_url + "/javascripts/eshop_js/shop_for_free_module.js"); }); </script> 

... hope this helps.

+2
source share

try it

Question: My first problem with understanding Railscasts sample code is how they define an action. If I wanted to call the createLike action from my lik_controller, what would I call it?

Answer:

 class LikesController < ApplicationController def create_like Like.create(:user_id => current_user.id, :item_id => params[:id]) end end 

in routes.rb file

 get '/create_like.js' => 'likes#create_like' 

Question: Secondly, my attempts so far have not worked, because both JavaScript files are not loading, and the action is also not called.

Anaswer: move app / assets / javascripts / likes / index.js.erb

the code

app / views / likes / create_like.js.erb

you need to pass item_id to

getScript method

 $(document).ready(function() { $('#list > li').dblclick(function(){ // styling $(this).toggleClass('liked'); // pass ID to controller var item_id = $(this).attr("data-id"); $.getScript("/create_like.js?item_id=" + item_id); }); }); 
+2
source share

All Articles