Flex on Rails 实例(一)

[size=medium]以下的例子我们在ubuntu下,(例子引用Manning.Flexible.Rails)
netbeans6.1 +rails2.1+Flex3 SDK+ruby1.86环境下执行

创建一个项目命名为:pomodo[/size]
~/NetBeansProjects$ rails pomodo
~/NetBeansProjects$ cd pomodo
~/NetBeansProjects/pomodo$ rake rails:freeze:gems
[size=medium]项目创建好了,rails版本也lock了

下面使用一个restful_authentication插件[/size]
~/NetBeansProjects/pomodo$ ./script/plugin install -r 3072 http://svn.techno-weenie.net/projects/plugins/restful_authentication/

+ ./README
+ ./Rakefile
+ ./generators/authenticated/USAGE
...output omitted...

[size=medium]确定已经显示以上信息,这个插件是创建用户登录功能的
下面我们通过插件生成了user和sessions[/size]
~/NetBeansProjects/pomodo$./script/generate authenticated user sessions
[size=medium]修改user的迁移文件[/size]
db\migrate\20081007123744_create_users.rb
class CreateUsers < ActiveRecord::Migration
def self.up
create_table "users", :force => true do |t|
t.column :login, :string
t.column :email, :string
t.column :first_name, :string, :limit => 80
t.column :last_name, :string, :limit => 80
t.column :crypted_password, :string, :limit => 40
t.column :salt, :string, :limit => 40
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :remember_token, :string
t.column :remember_token_expires_at, :datetime
end
end
def self.down
drop_table "users"
end
end



[size=medium]修改数据库配置文件[/size]
database.yml
development:
adapter: mysql
encoding: utf8
database: pomodo_development
username: root(输入自己正确的mysql用户名)
password: root(输入自己正确的mysql密码)
socket: /var/run/mysqld/mysqld.sock

[size=medium]迁移文件[/size]
~/NetBeansProjects/pomodo$rake db:create:all (创建数据库)
~/NetBeansProjects/pomodo$rake db:migrate(执行迁移)
~/NetBeansProjects/pomodo$ mysql -uroot -p

mysql> show databases;
Database |
+-----------------------+
| information_schema |
| mysql |
| omdb_dev |
| pomodo_development |
| pomodo_production |
| pomodo_test
mysql> use pomodo_development;
mysql> select * from users;


[size=medium]修改模型user,增加如下内容[/size]
app\models\user.rb
...
# prevents a user from submitting a crafted form that bypasses
# activation
# anything else you want your user to change should be added
# here.
attr_accessible :login, :email, :password,
:password_confirmation, :first_name, :last_name
...



增加 RESTful routes

config\routes.rb
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation:
# first created -> highest priority.
...
# Sample resource route (maps HTTP verbs to controller actions
# automatically):
# map.resources :products
map.resources :users
map.resource :session
signup URL
map.signup '/signup', :controller => 'users',
:action => login URL
map.login '/login', :controller => 'sessions',
:action => 'new'
logout URL
map.logout '/logout', :controller => 'sessions',
:action => 'destroy'
# Sample resource route with options:
# map.resources :products,
# :member => { :short => :get, :toggle => :post },
# :collection => { :sold => :get }
...
end

Modifying the includes and before_filter
as instructed by the comments

[size=medium]app\controllers\sessions_controller.rb
app\controllers\users_controller.rb
分别在上面两个控制器中,删除include AuthenticatedSystem
把include AuthenticatedSystem加到app\controllers\application.rb

并将protect_from_forgery 注释[/size]
app\controllers\application.rb
# Filters added to this controller apply to all controllers in
# the application. Likewise, all the methods added will be
# available for all controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
include AuthenticatedSystem
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session
# store
# TODO - this will be uncommented once we explain sessions
# in iteration 5.
# protect_from_forgery
# :secret => 'dd92c128b5358a710545b5e755694d57'
end



[size=medium]完成修改后启动web服务器,我们这里用默认的webrick[/size]
~/NetBeansProjects/pomodo$ ./script/server webrick
[size=medium]使用http://localhost:3000/signup 访问注册用户
http://localhost:3000/login 登录[/size]
[size=large],下篇将是详细介绍flex编译[/size]
[img]/upload/attachment/45422/dd41905e-e153-3ed9-96a5-1f8880d7e098.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值