rails hit two


#counter_cache
in father table sons_count:integer
in son model belongs_to :father,:counter_cache => true

#select a category or create one
attr_accessor :new_category_one #virtual attribute
before_save a record: method_name
def method_name
create_fatherModelName(:name => new_category_one) unless new_category_one.blank?
end
#and valid is neccessary
def validate
if model_id.blank? && new_level_one.blank?
errors.add(:attr,:message =>"...")
end
end
#in views
<p>
<%= f.label :level %><br />
<%= f.collection_select(:level_id, Level.find(:all,:order => :id), :id,
:name,:prompt => "--select a level--") %>
or Create: <%= f.text_field :new_level_one %>
</p>

#paginate
cattr_reader :per_page #class variable
@@per_page = 15
def self.search(search,page)
paginate(:page => page,
:order => ["league_id,name"],
:conditions => ["name like ?","%#{search}%"])
end
#can be be do this
@category.@papers.paginate(:page => params[:page]) #hidden attr per_page use class variable

#Player.largethan returns an array includes player's age > 20
named_scope :largethan,:conditions => ["age > ?", 20 ]

#Paper belongs_to Category,then
Category has hidden attribute: category.paper_ids
#this is a array,in view can use like this
<%= check_box_tag "category[product_ids][]", product.id,
@category.products.include?(product)%>
<%= product.name %>
#in controller action update
@category = Category.find(params[:id])
params[:category][:product_ids]||=[]
#in new is not need like this
@category = Category.new(params[:category])
#would be ok

#yield and content_for
#yield can be used in layout
<%= yield :one %>
# action's html
<% content_for do %>
#code
<% end %>

#ajax
<%=link_to_remote league.name,:url => league ,:method => 'get'%>
page.replace_html "showplace",:partial => "show"

#很有用的 in_groups_of,帮助表格化列出内容,同时使用了dom_id,用cycle方法来使颜色间隔
<% @tasks.in_groups_of(5,false) do |row_tasks| %>
<tr class="<%= cycle "green","gray" %>">
<% for task in row_tasks %>
<td id="<%= task.dom_id %>"><%=h task.name %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值