每天一剂Rails良药知识库 |
||
|
最近更新文章
每天一剂Rails良药之Perform Calculations on Your Model Data
ActiveRecord::Calculations模块提供了model级的方法来得到model数据的count,sum,average,maximum和minimum值 例如: Person.count("age > 21") Person.average(:age) Person.minimum(:age) Person.maximum(:age) Person.av ...
Rails宝典八十五式:YAML配置文件
我们的程序中可能有一些参数配置,我们可以将这些配置放在外部YAML文件里而不必污染应用程序代码: # config/initializers/load_config.rb APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV] # application.rb def authentic ...
Rails宝典八十四式:Cookie Based Session Store
Rails 2.0将默认使用CookieStore来存储session数据,这符合SNA,很好 如果从Rails 1.x迁移到Rails 2.0,需要这样配置一下: # in environment.rb Rails::Initializer.run do |config| config.action_controller.session = { :session_key => '_stor ...
Rails宝典八十三式:Migrations in Rails 2.0
Migrations are now sexy in Rails 2.0! 创建当前environme的数据库 rake db:create 创建所有environme的数据库 rake db:create:all 创建和修改Model script/generate model task name:string priority:integer script/generate mi ...
Rails宝典八十二式:Rails 2.0之HTTP Basic Authentication
# products_controller.rb before_filter :authenticate protected def authenticate authenticate_or_request_with_http_basic do |username, password| username == "foo" && password == " ...
Rails宝典八十一式:Rails2.0之Fixtures尝鲜
Rails 2.0中Fixtures简单多了,看个例子: # products.yml couch: name: Couch price: 399.99 manufacturer: lazyboy categories: furniture tv_stand: name: TV Stand price: 149.95 manufacturer: highdeph categories: furni ...
Rails宝典八十式:Rails 2.0简化视图
Rails 2.0 里视图应该怎样写? views/products/index.html.erb: <%= render :partial => @products %> views/products/show.html.erb: <%= render :partial => @product %> _product.html.erb: < ...
Rails宝典七十九式:生成Named Routes
我们有如下三个近乎一样的named routes: # routes.rb ActionController::Routing::Routes.draw do |map| map.about_company 'about/company', :controller => 'about', :action => 'company' map.about_company 'about/pri ...
Rails宝典七十八式:生成PDF文档
1,install gem install pdf-writer 2,require & register # environment.rb require 'pdf/writer' Mime::Type.register 'application/pdf', :pdf 3,use # lib/product_drawer.rb def self.draw(products) ...
Rails宝典七十七式:禁止JavaScript时的Destroy
Rails里一般我们这样Destroy: <%= link_to "Destroy", project_path(project), :confirm => "Are you sure?", :method => :delete %> 但是当客户端浏览器禁止JavaScript时就呆了,没有confirm,直接给删除了 所以如果我们 ...
Rails宝典七十六式:使用scope_out插件
安装 ruby script/plugin install http://scope-out-rails.googlecode.com/svn/trunk/ 使用例子 # models/task.rb scope_out :incomplete, :conditions => ['complete=?', false], :order => 'name' # tasks_cont ...
Rails宝典之七十五式:复杂表单Part3
前两次学习了动态添加和删除project的多个tasks,这次来看看如何编辑project 其他页面不变,但是_task.rhtml改了: <!-- projects/edit.rhtml --> <% form_for :project, :url => project_path(@project), :html => { :method => 'put' ...
Rails宝典之七十四式:复杂表单Part2
这次来看看如何使用JavaScript和RJS来动态添加和删除表单域: 继续上次的例子,我们首先要在layout里引入Prototype.js <%= javascript_include_tag :defaults %> 然后是新建project的页面模板: <div id="tasks"> <%= render :partial =&g ...
Rails宝典七十三式:复杂表单Part1
来看一个典型的场景,Project这个Model的new表单,我们需要在创建Project对象时也创建一些Task对象 我们可以使用fields_for这个标签: <!-- new.rhtml --> <% form_for :project, :url => projects_path do |f| %> <p> Name: <%= f.text ...
Rails宝典七十二式:添加自己的Environment
Rails自带了development、test和production三个environments,而我们可以轻松添加自己的environment: 1,修改database.yml staging: adapter: mysql database: store_staging user: root password: host: localhost 2,添加enviromnets/stagi ...
群组知识库热门文章
最新评论
不能适应超过三层的的override,比如我有A,B,C三个模板,B在A的基础上添加自己的东西,C在B ...
mingliangfeng 评论了 Rails宝典之第八式: layout与content_for
mingliangfeng 评论了 Rails宝典之第八式: layout与content_for
[/b][i][/i][u][/u]引用[color=red][/color][size=medium ...
linjie_830914 评论了 Rails源码研究之ActionController:二,ro ...
linjie_830914 评论了 Rails源码研究之ActionController:二,ro ...