2008年6月23日 星期一

like widget, use javascript take some data from host


[In someone's blog]
Here need send some request to our host
<script src='http://your_host/controller_name/action_name?q=2357,2388,2324&id=anything'></script>

that will become
params[:q] = '2357,2388,2324'
params[:id]= 'anything'

[Our controller ]
We have to return javascript to do something like insert some content

def action_name
@element_id = params[:id]

targets_id = [params[:q].split(',')].flatten.slice(0,2).compact
targets = Target.find(:all, :conditions => { :target_id => targets_id})

#@content is some data we want to insert it, and we encode it by function to_json,
#content may have something that javascript may have some problems in handling
#So, we encode all content by to_json to javascript type data
@content = render_to_string(:partial => 'content_partial', :locals => {:targets => targets}).to_json

#we don't need layout
render :layout => false

#we should return javascript, so we change the header
response.headers['Content-Type']='text/javascript'
end

[View]
this javascript will be received by your blog to do something like insert some content
(function(){
var quote_data =<%= @content %>;
document.write('<div id="<%= @element_id%>">'+quote_data+'</div>');
})();

[content_partial]
We use the id to make our css style would be unique
<style>
<% eid = "##{@element_id}" %>
<%=eid%> .scroll {
background-image: url(http://host/public_inside/images/bg_scroll.gif);
background-repeat: no-repeat;
height: 8px;
width: 268px;
*width: 310px;
}
</style>

<div class="scroll" >
<MARQUEE direction="left" loop="true" scrollamount="4" scrolldelay="100" >
<% targets.each do |t| -%>
<a href="<%= back_link_url(:controller => 'target', :action => 'show', :id => t) %>" target="_blank">
</a>
<% end -%>
</MARQUEE>
</div>

[helper]
#We should insert the complete url to link back
def back_link_url(options)
request.protocol + request.host_with_port + url_for(options)
end

2008年6月10日 星期二

Desing construction

設計架構(table?!, database?!)時 應該是在model的角度去看 是不是有共通點, 是不是需要繼承 或者是mixin就好, 而不是站在效能的角度上, 用OO的角度去思考

object errors empty

有時候物件會被create, 但是其實有error
這時就可以用 errors.empty? 來判斷
record = @me.records.create
if record.errors.empty?
#@me.set_census
flash[:info] = "fjaiofjaijfi"
else
flash[:notice] = "fafjahibh"
end

js onclick

<a class='bbt_pen' 'href='#' id="+v+" username='"+record.data.user_name+"' onclick='init_message(this);'><img src='images/mailbox.png'/></a>

onclick='init_message(this)'
要這樣用onclick

validate, validate_length_of

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001328
validates_length_of :content, :in => 1..300, :on => :create, :if => lambda{ |article| article.user_id != 1 }

:if 只有在article.user_id 不等於 1時, 才會有這樣的validate,
:on 在create的時候

model Object attribute attr_accessor

validate_of_ooxx :email 就只是去用email的mehod去get 而才不管他是table column還是attribute

Object.get_something 也是

所以最常見的就是
attr_accessor :plain_password
validates_presence_of :plain_password, :if => :password_required?
validates_presence_of :plain_password_confirmation, :if => :password_required?
validates_length_of :plain_password, :within => 4..40, :if => :password_required?
validates_confirmation_of :plain_password, :if => :password_required?

def password_required?
password.blank? || !plain_password.blank?
end


----
attr_accessor 可以幫妳做get set
想要overwrite
def plain_password
#this is get method
end

def plain_password=(ooxx)
#this is set method
end

----
>> User.find(856).plain_password
=> nil
>> User.find(856).adfafafd
NoMethodError: undefined method `adfafafd' for #<User:0xb75ac17c>
from /usr/local//lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
from (irb):13
>>

如果有這個method(attribute?!) 叫了沒東西會是 nil, 可是如果是沒這method(attribute?!)就直接有exception

2008年6月9日 星期一

jQuery attribute

[] 裡面可以直接塞attribute 去找element

$('input[name="msg[to_user_id]"]').val(id);
<input id="msg[to_user_id]" type="hidden" value="1" name="msg[to_user_id]"/>

String strip

http://www.ruby-doc.org/core/classes/String.html

strip 用來去掉空白的method 還有rstrip, lstrip

string, to_time, parse

想把字串變成time object

>> ('2004/01/2').to_time
=> Fri Jan 02 00:00:00 UTC 2004 <<= 有UTC

>> Time.parse('2004/01/2')
=> Fri Jan 02 00:00:00 +0800 2004

find, all, first

find(:all) 找不到是 []
find(:first) 找不到是 nil

association extension method

http://blog.hasmanythrough.com/2006/8/19/magic-join-model-creation

has_many :targets, :foreign_key =>"target_type_id" do
def stock
find(:all, :conditions => 'target_cat_id = "STO" and status <> 0 and target_class <> "0"' )
end
end


其實可以直接在target model 建 self.stock 的method 也可以用, 我還不知道差在哪 @@

js split

http://www.w3schools.com/jsref/jsref_split.asp

html 是 string, 用空格當作分界轉成array, 取第二個
text = html.split(' ')[1];

form submit input

form submit時 哪些值才會被當作需要的參數傳到server勒?

只有用input tag 的才會被傳到server, 所以當有需要的參數就要放在input裡, 有些時候就需要自己建
$('div.hide-input').append("<input type=text value="+index+" name=item["+index+"][order] id=item["+index+"][order] />");

而且name id 兩個attribute缺一不可