ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2010年4月15日 星期四
Practical Rails Social Networking Site memo
#Ch2
def to_param
"#{id}-#{permalink}"
end
def show
@page = Page.find(params[:id].to_i)
end
#Some database servers (including MySQL) will automatically do this conversion for you. However,PostgreSQL and some others do not, so it is a good idea to add this to your controllers. You could also overridean ActiveRecord method
#Ch3
module LoginSystem
protected
def is_logged_in?
@logged_in_user = User.find(session[:user]) if session[:user]
end
def logged_in_user
return @logged_in_user if is_logged_in?
end
def logged_in_user=(user)
if !user.nil?
session[:user] = user.id
@logged_in_user = user
end
end
def self.included(base)
base.send :helper_method, :is_logged_in?, :logged_in_user
end
end
map.show_user '/user/:username',:controller => 'users',:action => 'show_by_username'
map.resources :users, :member => { :enable => :put }
<%= render :partial => 'role', :collection => @user.roles %>
Roles available:
<%= render :partial => 'role', :collection => (@all_roles - @user.roles) %>
#Ch4
def self.up
create_table :categories do |t|
t.column :name, :string
end
news_category = Category.create(:name => 'Site News')
change_column :articles, :category_id, :integer, :default => news_category
end
def self.down
change_column :articles, :category_id, :integer, :default => 0
drop_table :categories
end
def get_http_auth_data
username, password = nil, nil
auth_headers = ['X-HTTP_AUTHORIZATION', 'Authorization', 'HTTP_AUTHORIZATION',
'REDIRECT_REDIRECT_X_http_AUTHORIZATION']
auth_header = auth_headers.detect { |key| request.env[key] }
auth_data = request.env[auth_header].to_s.split
if auth_data && auth_data[0] == 'Basic'
username, password = Base64.decode64(auth_data[1]).split(':')[0..1]
end
return [username, password]
end
@request.env['HTTP_ACCEPT'] = 'application/xml'
@request.env['Authorization'] = 'Basic ' + Base64::b64encode('editor:12345')
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言