实际是在工作过程中假公济私的一个即兴滥涂。
在2.3.5里通过了,其他版本就不知道了。
module StubActiveRecord
class Base
def save
return valid?
end
def save!;end
def new_record?;false;end
def update_attribute;end
def self.human_name;"#{self.name.underscore}";end
def self.human_attribute_name(col)
"#{self.name.underscore}.#{col}"
end
def self.self_and_descendants_from_active_record;[self];end
#stub methods above
include ActiveRecord::Validations
def initialize(params={})
@errors = ActiveRecord::Errors.new(self)
end
end
end
require 'stub_active_record/base'
class UserRemote < StubActiveRecord::Base
NECESSARY_ATTR = [:name]
attr_accessor *NECESSARY_ATTR
validates_presence_of *NECESSARY_ATTR
validate :validate_via_api
def initialize(params={})
super
NECESSARY_ATTR.each do|attr|
instance_variable_set(:"@#{attr}", params[attr])
end
end
protected
def validate_via_api
# add other errors here.
end
end
class UsersController < ApplicationController
def new
@user = UserRemote.new
end
def create
@user = UserRemote.new(params[:user_remote])
if @user.save
#blablabla
else
render :action => 'new'
end
end
end
<% form_for @user do |form| %>
<%= form.error_messages %>
<%= form.text_field :name %>
<% end %>
分享到:
相关推荐
Rails3教程系列 DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY...
jquery-validation-rails, 对 Rails 资产管道的jQuery验证 :: 验证:: rails针对 Rails 资产管道的...安装这里 gem将以下行添加到项目的Gemfile 中:gem 'jquery-validation-rails'在你的终端中运行以下命令:cd ~/pa
jquery-rails, 一个 gem,用于自动使用jQuery和 Rails 3 railsjQuery 面向 Rails 如此伟大。这里 gem 提供:jQuery 1.7.2jQuery UI 1.8.18 ( 仅适用于 javascript )jQuery UJS适配器assert_select_j
Ruby+on+Rails+3+Tutorial.pdf 应用Rails进行敏捷Web开发第4版.pdf (Agile Web Development with Rails) Rails.Recipes.Rails.3.Edition.pdf
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
本资源是在Rails开发先行的时候确定可以运行的Rails工程,可以直接用此代码运行画面
rails3的英文文档
简介 Ruby On Rails 框架自它提出之日...Rails 是一个真正彻底的 MVC(Model-View-Controller) 框架,Rails 清楚地将你的模型的代码与你的控制器的应用逻辑从 View 代码中分离出来。Rails 开发人员很少或者可能从未遇到
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...
Rails3消息队列系统 Sidekiq
turbo-sprockets-rails3, 加速你的Rails 3资产 用于 Rails 3.2.x的涡轮链轮 通过只根据源文件的哈希来重新编译已经更改的资产,从而加快 Rails 3 rake assets:precompile的速度只编译一次以生成指纹和非打印的资产...
rails常用命令,例如新建rails项目,新建controller、model 等等
rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板 Rails 应用编辑器 Rails 应用程序编辑器 gem 安装一个 命令行 工具来从"食谱"的Collection 组装 Rails 应用程序。"你可以使用 rails_...
Rails3的ActiveRecord 查询API.doc
Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置
为了达成学习目标,本书使用了一个完整的方案:通过实例学习使用 Rails 从零开始创建一个真正的程序。如 Derek Sivers 在前言中所说的,本书内容采用线性TDD结构,需要从头开始按顺序读到结尾。如果你经常跳着阅读...
一个Rails应用程序,为DIY小型内容分发网络CDN提供支持
ruby on rails开发的初学者很适合的工具书,易上手,很实用,
chef-rails, 厨房设置一个可以与 Nginx 和 Rails 一起滚动的Ubuntu服务器 主厨 rails安装一个准备为 Ruby on Rails 栈准备的Ubuntu服务器:NginxPostgreSQLRedisMemcached带RVM的rubyPhusion乘客独立要求Ubuntu ...