HAMLである条件がtrueの時だけあるclassをタグに追加したい場合
.post{:class => ("gray" unless post.published?)}
HAMLである条件がtrueの時だけあるclassをタグに追加したい場合
.post{:class => ("gray" unless post.published?)}
jQueryでチェックボックスの状態を監視するイベントはclick eventを使う。
チェックされたかを判断するには$(“#some_checkbox”).is(‘:checked’)がいい。
CoffeeScriptでのサンプルコード:
$("#some_checkbox").click (e) -> if $(this).is(':checked') # actions when checked else # actions when not checkedchange eventも監視はできるが、取れる値は遅延されるみたいです。
The change event is sent to an element when its value changes. This event is limited to input elements, textarea boxes and select elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
参考:http://forum.jquery.com/topic/jquery-checkbox-checked-event
ポイントは2 hours agoのような時間のリンクをクリックすること。マウスオーバーしないとリンクのように見えないですが。。
参考:http://www.askdavetaylor.com/how_to_figure_out_individual_twitter_status_url_tweet_twhirl.html
さくらVPSでWordpressブログをやってます。そんなにアクセスがないはずなのに、さくらVPSに移行した当時は結構サーバが落ちました。topコマンドで見るとスワップが結構70%まで上がったりして、手動でapache再起動とかもやりました。。ネットでいろいろググッてそれなりにパフォーマンスチューニングした結果やっと安定して、同じサーバに2つのWordpressブログと一つのRailsアプリケーションを実行しています。
参考までにさくらVPSは一番安いの512MB、月1000円のプランで、平日だとこのブログの一日PVは大体500〜600です。
チューニングしてから結構時間が経ったのでだいぶ忘れました。Evernoteでのメモを貼ります。
効果が高い順で。
W3 Total Cache プラグインが一押しです。インストールも設定も簡単ですし、一番効果があります。
ページのキャッシュ、cssやjavascriptのマージと圧縮など、結構やってくれます。実際これだけ入れても感じるほど早くなるはずです。
httpd.confを編集します。いろいろ修正してみましたが、結果下記の設定にしました。この辺はググったら結構出ますが、それぞれの環境に合わせて試行錯誤したほうがオススメです。
<IfModule prefork.c> StartServers 5 MinSpareServers 5 MaxSpareServers 10 ServerLimit 64 MaxClients 64 MaxRequestsPerChild 50 MaxMemFree 2000 </IfModule>
/etc/php.ini
zlib.output_compression = On
APC
pecl install APC
ActionView/Helpers/AssetTagHelper、asset(画像、CSSなど)ホストのチューニングについての勉強メモです。
Gistのmarkdownはembedする時にうまくスタイルをレンダリングしてくれないようです。。。
orz….https://gist.github.com/1350279で見たほうがいいかも知れません。
## asset hostを指定image_tag("rails.png")のhelper methodで生成するリンクはデフォルトでは同じホストのpublicフォルダを指しています。それを変更したい場合は`config/environments/production.rb`の`ActionController::Base.asset_host`をいじります。
```rubyActionController::Base.asset_host = "assets.example.com"```
```erbimage_tag("rails.png")# => <img alt="Rails" src="http://assets.example.com/images/rails.png?1230601161" />```
## asset hostを複数指定ブラウザが一度に同一サーバには2つのコネクションしかできないそう(これは初めて知りました)で、asset同士のダウンロード完了するのを待たなければなりません。もし複数台のassetサーバがある場合は`assets%d.example.com`を使ってそれをコネクション数を増やせることができます。`%d`が指定されればRailsは0~3の4つの番号を付けて、並行して8つのコネクションができます。
```rubyActionController::Base.asset_host = "assets%d.example.com"```
```erbimage_tag("rails.png")# => <img alt="Rails" src="http://assets.example.com/images/rails.png?1230601161" />stylesheet_link_tag("application")# => <link href="http://assets2.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />```
### カスタマイズもっと自分でカスタマイズしたい場合は`source`のprocパラメータを使えます。
```rubyActionController::Base.asset_host = Proc.new { |source| "http://assets#{Digest::MD5.hexdigest(source).to_i(16) % 2 + 1}.example.com"}image_tag("rails.png")# => <img alt="Rails" src="http://assets1.example.com/images/rails.png?1230601161" />stylesheet_link_tag("application")# => <link href="http://assets2.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />```
特定のパスで始まるassetを特定のホストに指定する例:```rubyActionController::Base.asset_host = Proc.new { |source| if source.starts_with?('/images') "http://images.example.com" else "http://assets.example.com" end }```
```erbimage_tag("rails.png")# => <img alt="Rails" src="http://images.example.com/images/rails.png?1230601161" />stylesheet_link_tag("application")# => <link href="http://assets.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />```さらに`request`の第二パラメータもあります。これでHTTPSの動作も制御できます。
```rubyActionController::Base.asset_host = Proc.new { |source, request| if request.ssl? "#{request.protocol}#{request.host_with_port}" else "#{request.protocol}assets.example.com" end}```
## Resourceshttp://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
自分のマウスがスクリーンのどこにあるかを知りたいときに役立つアプリです。
ホットキーを押せば大きい緑色の圓でマウスポインタを示しので、すぐに分かります。
さらに素晴らしいことはパソコンロック解除されたときに自動でアクティブになるんです!ユーザエクスペリエンス!
例えば省略可能なフラグみたいなローカル変数をpartial viewに渡した時、そのデフォルト動作をpartial viewでハンドリングしたいですね。
<%= render 'article', :show_author => false %><%= render 'article' %>
調べてみたらdefined?と local_assigns.has_key?が見つかりましたが、前者のほうはオススメできなさそうです。
Testing using defined? headline will not work. This is an implementation restriction.
# http://api.rubyonrails.org/classes/ActionView/Base.html
# If you need to find out whether a certain local variable# has been assigned a value in a particular render call, # you need to use the following pattern:
<% unless local_assigns.has_key? :show_author %> <% show_author = true %><% end %>
<% if show_author %> Author: <%= @article.author %><% end %>
# Testing using defined? headline will not work. This is an implementation restriction.
実際やってみて、defined?でも動けるパターンはありますが、if/unless文で一行にしたらバグりました。なのでやはり local_assigns.has_key?を使いましょう。
# this will work<% unless defined?(:show_author) %> <% show_author = true %><% end %>
# NOT work<% show_author = true unless defined?(:show_author) %>
# this will work<% show_author = true unless local_assigns.has_key? :show_author %>http://stackoverflow.com/questions/238615/defined-method-in-ruby-and-rails