ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2010年4月20日 星期二
[Ptt][心得] Restful下遇到params[:id]含有.的解法
在restful的前提下, 遇到class的to_param是字串, 而剛好某筆
紀錄的to_param欄位含有.
一般情形下會發生錯誤, 因為restful會把.後的東西當成format
這時的解法, 變成要在routes.rb中的xxx.resources加上參數
ex:
map.resources :categories, :requirements => {:id => /.*/}
paramter change permanently, header status
if params[:id] != @episode.to_param
headers["Status"] = "301 Moved Permanently"
redirect_to episode_url(@episode)
else
@comment = Comment.new(:episode => @episode)
respond_to do |format|
format.html
format.rss
end
end
2010年4月16日 星期五
Practical Rails Social Networking Site memo (2)
#Ch10
module ApplicationHelper
def yes_no(bool)
...
end
def tag_cloud(tags, classes)
max, min = 0, 0
tags.each do |tag|
max = tag.count if tag.count > max
min = tag.count if tag.count < min
end
divisor = ((max - min) / classes.size) + 1
tags.each do |tag|
yield tag.name, classes[(tag.count - min) / divisor]
end
end
end
<% tag_cloud @tags, %w(tag1 tag2 tag3 tag4 tag5) do |name, css_class| %>
<%= link_to name, tag_path(name), :class => css_class %>
<% end %>
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')
2010年4月6日 星期二
check timezone valid
module TimeFunc
def self.included(base)
base.class_eval do
base.extend Methods
end
end
module Methods
def check_timezone_legal(timezone)
raise IllegalTimeZone unless ActiveSupport::TimeZone::ZONES.map(&:name).include?(timezone)
end
end
end
較新的文章
較舊的文章
首頁
訂閱:
文章 (Atom)