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}})
}

 

Connect to a remote MySQL server with MySQLBrowser using SSH

Problem

You would like to connect to a remote MySQL server using the Mysql-query-browser GUI tool, and you only have localhost access to the remote server, but also have ssh access to the remote server.

 

Solution

You can use the following command to start a port forwarding to your localhost and then use that to connect to the remote database server:

ssh -L 3307:localhost:3306 user_name@remote_host -N

You could then use the following to the Mysql-query-broser connection:

 

Hostname: 127.0.0.1
username: db_user_name_on_remote
password: db_user_password_on_remote
port: 3307

Replacing mock objects with factory girl in controller rspecs

Problem

You would like to use factory_girl instead of the mock models described in the RSpec book (Behaviour Driven Rails – Rails Controllers – Controller specs).

 

Solution

Make sure that you use let! instead of let and have the following:

 

require 'spec_helper'

describe MessagesController do
  describe "POST create" do
    let!(:message) { Factory.create(:message) }
    
    before do 
      Message.stub(:new).and_return(message)
    end
    
    it "creates a new message" do
      Messagee.should_receive(:new).with("text" => "a quick brown fox").and_return(message)
      post :create, :message => {"text" => "a quick brown fox"}
    end
    
    it "saves the message" do
      message.should_receive(:save)
      post :create
    end

    it "redirects to the Messages index" do
      post :create
      response.should redirect_to(:action => "index")
    end
  end
end

Parent task translation missing: en, activerecord, errors, models, issue, attributes, parent_issue_id, not_a_valid_parent

Problem
You get the following error in redmine when trying to update a task that has a parent task.

Parent task translation missing: en, activerecord, errors, models, issue, attributes, parent_issue_id, not_a_valid_parent

Solution
It seems that for some reason when you add more than one subtask, the lft and rgt id numbers are not created correctly so you cannot update the subtasks.
If the numbers are overlapping (rgt for first subtask 2 and lft for second subtask 2), then there is a problem.
So in order to be able to solve that you will need to change the lft, rgt ids to have subsequent numbers as:
first subtask: lft -> 1 , rgt -> 2
second subtask: lft -> 3, rgt -> 4

ERD diagrams in Ruby on Rails

Problem

You would like to have an ERD diagram of your database in your Ruby on Rails project.

Solution

You can install the Rails ERD from here, and install it by following the instructions.
For Ubuntu/debian systems should be:

sudo apt-get install graphviz

and then adding the gem to your Gemfile in the development section as:

group :development do
  gem "rails-erd", "~> 0.4.5"
end

You can then run bundle install to install the gem and rake erb to create the diagram pdf.

Pausing between steps in cucumber

Problem

You want to be able to go through a scenario step by step when you are testing with a browser as sometimes can be very fast.

 

Solution

By following the suggestion here you would only need to add the following to your features/support/hooks.rb file

AfterStep('@pause') do
  print "Press Return to continue ..."
  STDIN.getc
end

and then you should be able to use it in your scenario by adding the @pause tag.
Then you would have to press continue in your terminal to go step by step