rails on mysql

หลังจากลง mysql-server, mysql-client, libmysqlclient15-dev

$ gem install mysql
$ mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'dsin'@'localhost'

1. dump data from .sql using rake

lib/tasks/sample_data.rake

# Provides task to load sample data.

require 'active_record'
require 'active_record/fixtures'

namespace :db do
DATA_DIRECTORY = "#{RAILS_ROOT}/lib/tasks/sample_data"
namespace :sample_data do

desc "Load sample data"
task :load => :environment do |t|
chdir DATA_DIRECTORY
system 'mysql lectures_development < lectures.sql'
end
end
end
$ rake db:sample_data:load

2. ลง dojo-release-1.2.1.tar.gz ไปที่ public/javascript

/layouts/application.html.erb —

<%= stylesheet_link_tag "/javascripts/dojox/grid/resources/Grid.css" %>
<%= stylesheet_link_tag "/javascripts/dojox/grid/resources/tundraGrid.css" %>
<%= stylesheet_link_tag "/javascripts/dijit/themes/tundra/tundra" %>
<%= stylesheet_link_tag "/javascripts/dojo/resources/dojo.css" %>
<%= javascript_include_tag 'dojo/dojo', :djConfig => 'isDebug:false, parseOnLoad:true' %>

3. ถ้า javascript turn off จะไม่ทำใน noscript

<noscript>

<table> ... </table>

</noscript>

4.

dojo.data.ItemFileWriteStore

structure -> HTML table

—————————————-

<script type=”text/javascript”>
dojo.require(“dojo.data.ItemFileWriteStore”);

dojo.require(“dojox.grid.DataGrid”);

dojo.require(“dojox.grid.cells”);

var gridStore = new dojo.data.ItemFileWriteStore({
url: ‘/courses/<%= @course.id %>/lectures.json’,   # ajax ไปเอาข้อมูล JSON มาจาก URL นี้ แล้วโหลดเข้า structure ( Model )
id: ‘gridData’
});
var grid = null;

////////////////////////////////////////////////////////////////////////////

function saveGrid(){
gridStore.save();
}


// extend dojo.data.ItemFileWriteStore

dojo.declare(“CustomItemFileWriteStore”, dojo.data.ItemFileWriteStore, {
_saveCustom: saveCustom,
_sendUpdate: sendUpdate,
_formEncode: formEncode,
});


var saveCustom = function (saveCompleteCallback, saveFailedCallback) {
var success = true;
if (this.isDirty()) {
if (!this._isEmpty(this._pending._newItems)) {
success = this._sendCreate(this._pending._newItems);
}
if (success && !this._isEmpty(this._pending._deletedItems)) {
success = this._sendDelete(this._pending._deletedItems);
}
if (success && !this._isEmpty(this._pending._modifiedItems)) {
for (identity in this._pending._modifiedItems) {
var modifications = null;
// identity is the ORIGINAL value; we need the modified items too
if (this._itemsByIdentity) {
modifications = this._itemsByIdentity[identity];
} else {
modifications = this._arrayOfAllItems[identity];
}
success = this._sendUpdate(identity,modifications);
}
}
}
if(success) {
saveCompleteCallback();
} else {
saveFailedCallback();
}
}


function formEncode(item) {
var id = ‘lecture[id]=’ + item.id;
var dates = ‘&lecture[lecture_dates]=’ + escape(item.lecture_dates);
var number = ‘&lecture[lecture_number]=’ + escape(item.lecture_number);
var topics = ‘&lecture[topics]=’ + escape(item.topics);
var readings = ‘&lecture[readings]=’ + escape(item.readings);
var auth = ‘&’ + window._auth_token_name + ‘=’ + window._auth_token;
return id + dates + number + topics + readings + auth;
}


var sendUpdate = function(origItem, newItem){
var putData = this._formEncode(newItem);
return dojo.rawXhrPut({
preventCache: true,
url: “<%= course_lectures_path( @course ) %>/” + origItem + ‘.json’,
handleAs: “text”,
putData: putData,
sync: true,
timeout: 10000,
load: function(response, ioArgs){
return true;
},
error: function(response, ioArgs){
var errors = eval( ‘(‘ + response.responseText + ‘)’ );
alert(‘Error:’ + errors.base);
return false;
}
});
}

var gridStore = new CustomItemFileWriteStore({
url: ‘/courses/<%= @course.id %>/lectures.json’,   

id: ‘gridData’

});

///////////////////////////////////////////////////////////////////////////

dojo.addOnLoad( function() {
// Add style to the gridContainerOuter
var gridContainerOuterNode = dojo.byId(“gridContainerOuter”);
gridContainerOuterNode.setAttribute(“style”, “background:maroon; padding:0.5em; width:50em;”);
// Add style to the gridContainerInner
var gridContainerInnerNode = dojo.byId(“gridContainerInner”);
gridContainerInnerNode.setAttribute(“style”, “background:#fff; height:350px;”);
// Add style to the gridNode
var gridNode = dojo.byId(“gridNode”);
gridNode.setAttribute(“style”, “border: 1px solid #333;”);
// Create the column layout for the grid
var layout = [
{name: “Lecture number”, field: “lecture_number”, width: “3em”, editable: true, type: dojox.grid.cells.Editor },
{name: “Dates”,          field: “lecture_dates”,  width: “5em”, editable: true , type: dojox.grid.cells.Editor },
{name: “Topics”,         field: “topics”,         width: “35%”, editable: true , type: dojox.grid.cells.Editor },
{name: “Readings”,       field: “readings”,       width: “35%”, editable: true , type: dojox.grid.cells.Editor },
];

// editable: true — double click on cell and can edit

//  type: dojox.grid.cells.Editor — rich-text html editor

// ( dojo.require(“dojox.grid.cells”); )


// Create the grid
grid = new dojox.grid.DataGrid({
query: { id: ‘*’ },   #
store: gridStore, #
structure: layout
}, ‘gridNode‘);
grid.startup();

// Create the save button , callback to saveGrid()
saveButton = new dijit.form.Button({label:’Save changes’, onClick:saveGrid});
dojo.byId(‘scheduleGrid’).appendChild(saveButton.domNode);


});
</script>
<div id=”scheduleGrid”>
<div id=”gridContainerOuter”>
<div id=”gridContainerInner”>
<div id=”gridNode“></div>
</div>
</div>
</div>

5. extend ActiveRecord::Errors

application.rb —

class ActiveRecord::Errors
def to_json
@errors.to_json
end
end

how to update gem

หลังจากเจอปัญหา

Rails requires RubyGems >= 1.1.1 (you have 0.9.4). Please `gem update –system` and try again.

$ sudo gem update --system

$ sudo gem install -v=2.1.1 rails

$ gedit /usr/bin/gem
require 'rubygems'

require 'rubygems/gem_runner'  ## -- add this line in

ใน environment.rb แก้

RAILS_GEM_VERSION = '0.9.4'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s