2008年9月30日 星期二

ie6 z-index jquery bgiframe

http://jsgears.com/thread-74-1-1.html
http://plugins.jquery.com/project/bgiframe

2008年9月27日 星期六

length, count

SELECT length(todo_name) as l FROM records order by l limit 1;

SELECT COUNT(*) FROM records where todo_name = 'wake_up';

2008年9月22日 星期一

url, post, Net, http


url_host = 'iwakela.com'
url_path = '/alert/sms'
url_port = 9001
auth_string = "您的手機警示已經到價"
data = "dstaddr=" + cell_number + \
"&message=" + auth_string
headers = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' }
http = Net::HTTP.new(url_host, url_port)
resp, resp_data = http.post(url_path, data, headers)
save_sms_to_record(cell_number, auth_string, resp_data)

2008年9月21日 星期日

The rails way ch3 routing


trigger a branching on respond_to by setting the HTTP-Accept header in the request
p70.

wget http://localhost:3000/items/show/3 -0 - -header="Accept:text/xml"



The Empty Route

map.root :controller => "homepage"



Using Regular Expression in Routes

map.connect ':controller/show/:id',
:action = "show", :requirements => {:id => /\d+/}



Route Globbing & key-value pairs

map.connect 'items/*specs', :controller => "items", :action => 'specify'

def specify
@items = Item.find(:all, :conditions => Hash[params[:specs]])
...
end


2008年9月18日 星期四

include, map


user 拿全部的x_alerts

[model]
belongs_to :user
has_many :x_alerts, :extend => XAlert::AssociationMethods, :through => :x_alert_relationships
has_many :x_alert_relationships



a = u.alert_targets.find(:all, :include => {:x_alert_relationships => :x_alert} )

AlertTarget Load (0.000180) SELECT * FROM `alert_targets` WHERE (`alert_targets`.user_id = 2590)
XAlertRelationship Load (0.000152) SELECT `x_alert_relationships`.* FROM `x_alert_relationships` WHERE (`x_alert_relationships`.alert_target_id IN (1,2,10,11))
XAlert Load (0.000212) SELECT * FROM `x_alerts` WHERE (`x_alerts`.id IN ('6','216','7','8','250','229','218','241','197','214','104','5'))

a.map {|e| xar = e.x_alert_relationships; xar.map { |k| h = k.x_alert; h[:cat] = k.created_at; h}}.flatten

2008年9月8日 星期一

The rails way ch2

render, redirect_to, 一個action 裡面只能有一個結果

render, redirect_to 之後的動作也是會繼續做, 這個action的flow跑完, 才會去redirect_to 那的action, 若是不想把flow跑完就得接return

redirect_to :controller => 'main' and return



prepend_before_filter
skip_filter

2008年9月7日 星期日

redirect_to

http://api.rubyonrails.org/classes/ActionController/Base.html#M000852
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

  redirect_to post
redirect_to :action => "show", :id => 5
redirect_to "http://www.rubyonrails.org"
redirect_to "/images/screenshot.jpg"
redirect_to articles_url
redirect_to :back

The redirection happens as a "302 Moved" header unless otherwise specified.

Examples:

  redirect_to post_url(@post), :status=>:found
redirect_to :action=>'atom', :status=>:moved_permanently
redirect_to post_url(@post), :status=>301
redirect_to :action=>'atom', :status=>302

:back - Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to(request.env["HTTP_REFERER"])



301 Moved Permanently
This and all future requests should be directed to the given URI.

302 Found
This is the most popular redirect code, but also an example of industrial practice contradicting the standard. HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"), but popular browsers implemented it as a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to disambiguate between the two behaviours. However, the majority of Web applications and frameworks still use the 302 status code as if it were the 303.
303 See Other (since HTTP/1.1)
The response to the request can be found under another URI using a GET method.
307 Temporary Redirect (since HTTP/1.1)
In this occasion, the request should be repeated with another URI, but future requests can still use the original URI. In contrast to 303, the request method should not be changed when reissuing the original request. For instance, a POST request must be repeated using another POST request.

2008年9月4日 星期四

$.each


This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.

The callback has two arguments:the key (objects) or index (arrays) as the first, and the value as the second.

If you wish to break the each() loop at a particular iteration you can do so by making your function return false. Other return values are ignored.

var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };

jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});

jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});

My id is one. - 1
My id is two. - 2
My id is three. - 3
My id is four. - 4
- 5



#Iterates over items in an array, accessing both the current item and its index.

$.each( [0,1,2], function(i, n){
alert( "Item #" + i + ": " + n );
});



#Iterates over the properties in an object, accessing both the current item and its key.

$.each( { name: "John", lang: "JS" }, function(i, n){
alert( "Name: " + i + ", Value: " + n );
});

jquery each find what is different


[html]
<div id="'test'">
<span id="'each'">(click here to change)</span>
<ul>
<li class="cc">Eat</li>
<li class="cc">Sleep</li>
<li class="bb">Be merry</li>
</ul>
</div>

[script 1]
$("#test span").click(function () {
if ($("#test").find('li')) {
alert($(this).html());
}
});
$(this) is $(#test span)
[script 2]
$("#test span").click(function () {
if ($("#test li")) {
alert($(this).html());
}
});

$(this) is $(#test span)


[script 3]
$("#test span").click(function () {
$("#test li").each(function(i, e){
alert($(this).html());
}
});

$(this) is really every li tag
i is the iterator counter
e == $(this)



Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).

render page update


[view]
function ajax_create(act, pos, goals) {
$.ajax({
url: '/goals/'+act,
type: 'POST',
data: goals,
dataType: 'html',
success:function(html){
$(pos).after(html);
$('td.goal_del').unbind('click', goal_del).bind('click', goal_del);
$('td.goal_up_tmp').unbind('click', goal_up ).bind('click', goal_up);
$('td.goal_down_tmp').unbind('click', goal_down).bind('click', goal_down);
}
});
}

[controller]
render :partial => 'member/temp_goal', :locals => {:g => new_goal}



[view]
<%= observe_field :target_type_target_type_id, :on => "change",
:url => {:action => :update_target_list},
:with => "{target_type_id: value, target_cat_id: $('target_type_target_cat_id').value}"%>

(用page 寫 js)
[controller]
render :update do |page|
page.replace_html 'target_type_target_id', :partial => 'target_type_select', :locals => {:types => target_list}
end



[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
success : function(html){
alert(html);
}
});
}

[controller]
render :action => 'test', :layout => false

[test view]
<% response.headers['Content-Type'] = "text/javascript" %>
alert(<%=1+1 %>);




[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
dataType: 'script'
});
}

[controller]
render :action => 'test', :layout => false

[test view]
<% response.headers['Content-Type'] = "text/javascript" %>
alert(<%=1+1 %>);




[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
dataType: 'script'
});
}

[controller]
render :update do |page|
page.alert('cccccc') #page << "alert('cccc');" the same end




[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
dataType: 'script'
});
}

[controller]
def test
#nothing
end
[test view] test.rjs
page << "alert('cccccc');"

2008年9月1日 星期一

error message 中文化

http://wiki.rubyonrails.org/rails/pages/HowToLocalizeActiveRecordErrors
http://wiki.rubyonrails.org/rails/pages/OverridingRailsMessagesInAnotherLanguage
http://lightyror.thegiive.net/2006/08/rails-error-message.html

model, include, left outer join


>>User.find(:all, :include => 'profile')
User Load (0.000682) SELECT * FROM `users`
Profile Load (0.000492) SELECT `profiles`.* FROM `profiles` WHERE (`profiles`.user_id IN (1,2,4,5))
Profile Columns (0.000880) SHOW FIELDS FROM `profiles`


>> User.find(:all, :include => 'profile', :conditions => "users.name is not NULL")
User Load (0.000731) SELECT * FROM `users` WHERE (users.name is not NULL)
Profile Load (0.000183) SELECT `profiles`.* FROM `profiles` WHERE (`profiles`.user_id IN (1,2,4,5))


User.find(:all, :include => 'profile', :conditions => "profiles.others is NULL")
User Load Including Associations (0.001349) SELECT `users`.`id` AS t0_r0, `users`.`name` AS t0_r1, `users`.
`password_salt` AS t0_r2, `users`.`password_hash` AS t0_r3, `users`.`email` AS t0_r4, `users`.`created_at` AS t
0_r5, `users`.`cookie_hash` AS t0_r6, `users`.`target_time_now` AS t0_r7, `users`.`reset_password_code` AS t0_r
8, `users`.`reset_password_code_until` AS t0_r9, `users`.`yahoo_userhash` AS t0_r10, `users`.`group_id` AS t0_r
11, `users`.`group_nickname` AS t0_r12, `users`.`time_zone` AS t0_r13, `profiles`.`id` AS t1_r0, `profiles`.`user_id` AS t1_r1, `profiles`.`others` AS t1_r2, `profiles`.`sex` AS t1_r3, `profiles`.`birth` AS t1_r4, `profiles`.`star` AS t1_r5, `profiles`.`blood` AS t1_r6, `profiles`.`address` AS t1_r7, `profiles`.`school` AS t1_r8, `profiles`.`job` AS t1_r9, `profiles`.`interest` AS t1_r10, `profiles`.`photo` AS t1_r11, `profiles`.`connect` AS t1_r12, `profiles`.`email_weekly` AS t1_r13 FROM `users` LEFT OUTER JOIN `profiles` ON profiles.user_id = users.id WHERE (profiles.others is NULL)