r2 - 10 Mar 2007 - 12:26:07 - CharinyaKlakhangYou are here: SETEC Wiki >  Knowledge Web  > WebTechnologyCategory > RubyOnRails > RubyOnRailsTipAndTrick

Ruby On Rail Trick And Trip

1. Tag link image :

<%= link_to image_tag("search.gif", :border=>0), :action => 'show', :id => user %> 

Description : Make to create link to a image


2. แสดงผลภาษาไทย :

 <% @headers["Content-Type"] = "text/html; charset=tis-620" %>


Displays a image inside link_to instead of text

<%= link_to image_tag("search.gif", :border=>0), :action => 'show', :id => user %>


ดึงข้อมูลใน DB มา 1 row

--controller:login_controller.rb , action: list_users--

@login_user = User.find(:first, :conditions => "id like '%#{session[:user_id]}'" )

--view : list_users.rhtml--

@login_user.name == user.name


การเอา page อื่น มาแสดงผลร่วมกัน

  • View: /*แทรก page อื่นร่วมแสดงด้วย*/

<%= render_component( :controller => "login",
                     :action => "index") %>
  • Controller: /*ไม่ให้แสดง layout ซ้อนกัน*/

if params[:context] == :ship
      render(:layout => false) 
end


การลบรายการสินค้าในตะกร้าใน display_cart

1. วนลูปเก็บ id (ตัวแปร "i")ของ สินค้า ใน display_cart.rhtml
2. <%= link_to("(ยกเลิก)", :action => :delete_item,:id => i )%> <--  ส่ง ค่าของตัวแปร "i" 
   
3. เขียน action : delete_item ที่ controller:store 
 def delete_item
     @cart = find_cart                              
     iid = params[:id].to_i                        # convert id ที่ส่งมาเป็น int
     flash[:notice] = "ลบหนังสือออกจากตะกร้าแล้ว"
     @cart.remove_qt(iid)                        # เรียก method ใน cart.rb
     @cart.items.delete_at iid                   # ลบ array ตัวที่ iid
     redirect_to(:action => 'display_cart')
  end

4. เพิ่ม method ใน model: cart.rb  
 def remove_qt(id)     
      count = (@items[id].unit_price)*(@items[id].quantity)
      @total_price = @total_price - count
  end

NOTE: @cart.items.class  --> .class : จะ print ชนิดของตัวแปรนั้นว่าเป็นชนิดอะไร เช่น array , class
           params[:id].to_i   --> .to_i   : จะ convert str to int


ใช้ Javascript ช่วยในการแสดงผล


1. ต้องมีไฟล์ prototppe.js ที่ /public/javascript

2. <%= javascript_include_tag "prototype" %> เพิ่มในไฟล์ .rhtml ที่ต้องการให้แสดงผล

   <%= link_to_remote("[แก้ไขจำนวน]",:update => 'mydiv',:url => {:action => :add_quantity,:id =>@items.index(item)}) %> 
 
   <div class="mydiv"> ...พื้นที่ที่ต้องการให้หน้าของ add_quantity.rhtml มาแสดผลที่นี่... </div>


NOTE : @items.index(item)  return ตัวเลขของ array


pop new window

<%= link_to "หน้าแรก" ,{ :controller => "login", :action => "index"},{ :target => '_blank' } %>


การแปลง

String#to_i    String  to   Integer
String#to_f    String to   Float
Float#to_i    Float  to   Integer
Float#to_s    Float  to   String
Integer#to_f   Integer to Float
Integer#to_s   Integer to String


operator

==     equal
!=    not equal to
>    greater than
<    less than
>=    greater than or equal to
<=    less than or equal to


condition

if age >= 60
    puts "Senior fare"  
elsif age >= 14
    puts "Adult fare"
elsif age > 2
    puts "Child fare"
else
    puts "Free"
end


เช็คว่าเป็นตัวเลขหรือไม่

@number.integer?
interger?   ==>   เช็คว่าเป็นตัวเลขหรือไม่


การลบ + confirm

<%= link_to "[ลบ]",{:action => :delete_item,:id => @items.index(item)}, :confirm => 'Are you sure?'%>


การใช้ Ajax

<%= link_to_remote( "click here",
                   :update => "time_div",
                   :url => { :action => :say_when },
                   :position => "after" ) %>

NOTE:

:position        ===>                    before, after, top, and bottom.
:position => top and bottom         insert inside of the target element, 
:position =>  before and after       insert outside of the target element.


Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions
 
Powered by SETEC Wiki
Copyright ©2012 by National Electronics and Computer Technology Center, NECTEC.
Ideas, requests, problems regarding SETEC Wiki? Send feedback