Adding a record in Rails through javascript

Problem

You would like to trigger a record insertion after a specific action has happened through javascript (ie drag – drop an item into a container), in a rails application.

 

Solution

You would need to get the object attributes from the html page somehow (ie user_id and user_name), and then have a function in your application js that calls the post function.

So if you have a drag and drop container for example then the code in the application.js should be like the following:

$("#name_of_container").droppable({
  drop: handleDropEvent
});

function handleDropEvent(event,ui) {
  var user_id = ui.draggable.attr("user_id");
  var user_name = ui.draggable.attr("user_name");
  $.post('/users', {user: {user_id: user_id, name: user_name}})
}

 

setting dynamic event_source in jquery fullcalendar in rails application

Problem

You would like to use the fullcalendar jquery plugin to be able to display events in your rails application, but you also want to be able to set the eventSources dynamically depending on the path to your view, especially if your view contains a relationship as in the following example:

model_a/1/model_b (user/14/comments)

Solution

Change your eventSources to be something like the following and using the jQuery.ajaxSettings.url :

 // a future calendar might have many sources.
    eventSources: [{
      url: jQuery.ajaxSettings.url,
      color: 'orange',
      textColor: 'black',
      ignoreTimezone: false
    }],

 

Could not find generator jquery:install

Problem
When you try to replace prototype with jquery in your Rails 3.0.x application, by using the command described in the Agile Web development book:

rails generate jquery:install --ui --force

you get the following error:

Could not find generator jquery:install

Solution
You would need to follow the steps below:

  1. Add gem “jquery-rails”, “~> 1.0.13” in your Gemfile, and run bundle install
  2. run the command described above: rails generate jquery:install –ui –force

Now you should be able to see something like:

remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.6.2)
create public/javascripts/jquery.js
create public/javascripts/jquery.min.js
copying jQuery UI (1.8.14)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (cd619d)
remove public/javascripts/rails.js
create public/javascripts/jquery_ujs.js

Using Flexigrid with MySQL field carriage returns

Problem
You want to use flexigrid with a MySQL fields that contains carriage returns. As the flexigrid uses json it doesn’t work with carriage returns, and displays an empty page, instead of an error page.

Solution
In the page_name.json.erb file in the fields that has carriage returns make sure that you use to_json method as in the example below:

‘<%=fg_escape event.comments.to_json -%>‘,