RJSテンプレートは、RubyコードからJavaScriptコードを生成 するための仕組みです。 今回は、RJSテンプレートの中でJavaScriptの条件分岐を 簡潔に記述するために、PrototypeHelperを拡張する方法を紹介します。

以下のようなファイルを作成して、config/initializersの下に 設置します(Rails 2.0以降の場合)

prototype_helper_ext.rb

   1  module ActionView
   2    module Helpers
   3      module PrototypeHelper
   4        class JavaScriptGenerator
   5          def if(condition, &block)
   6            page << "if(#{condition}){"
   7            block.call if block
   8            page << '}'
   9          end
  10  
  11          def elsif(condition, &block)
  12            page << "else if(#{condition}){"
  13            block.call if block
  14            page << '}'
  15          end
  16  
  17          def else(&block)
  18            page << "else{"
  19            block.call if block
  20            page << '}'
  21          end
  22        end
  23      end
  24    end
  25  end

これによって、こんな感じに条件分岐を記述できるようになります。

   1  render :update do |page|
   2    page.if "$$('#posts .post').length == 0" do
   3      page.remove 'posts'
   4    end
   5  end

最初から用意されててもいい気がする。

posted by Png genki on Sat 5 Jan 2008 at 16:49

不定期に小ネタを並べていくかもです。

  • エクスプローラで圧縮
    ファイル右クリック-送るで「圧縮(zip形式)フォルダ」

  • Adobe Acrobat Readerの読み上げ機能
    表示 - 読み上げ - 読み上げを起動
    表示 - 読み上げ -このページのみを読み上げる
    この機能、日本語のPDFではうまく起動しましせん。 試しにRailsRecepiのpdfを読ませてみたら、ネイティブだと分かるようなレベルか。ムズカシイ。

posted by Png satoko on Sat 5 Jan 2008 at 07:28

You can see also http://blog.s21g.com/articles/212, the article written about the same theme for comparison, if you are familiar to Japanese.

1. p/pp

This is a very popular method used not only in Rails but also in other debugging. In Rails this method should be used on web server which is in the states of foreground process.

   1   $ ./script/server

If you are familiar with p/pp, it should be simple and easy.

2. logger.debug

For logger.debug, the following example will explain fully.

   1  logger.debug "something interesting information"

Calling logger.debug, you can create a log file of log/$RAILS_ENV.log such as log/development.log.

By configuring the output file to:

   1  $ tail -f log/development.log

you can check the debug output log in the same manner as in p/pp debugging. Furthermore, as the result is output in a log-file format, you can refer it later on.

3. script/console

script/console comes in handy when you check the DB contents during the operation. Executing "script/console" command, irb launches after confirming Rails environment. You can change DB as you like by using ActiveRecord, such as find, create and destroy_all.This is very tactful feature.

4. script/breakpointer

As previously described, although script/console comes in very handy and tactful, it can not check session status during the action. In that case, you can use "script/breakpointer" for it.

   1  $ ./script/breakpointer

Please make sure to launch a breakpointer initially. Then locate a "breakpoint" to the position where you want to check the status.

   1  class FooController < ApplicationController
   2    def bar
   3      # something
   4      breakpoint
   5      # something
   6    end

Then you execute bar action through brower access and check and manupulate the necessary information after the launch of irb at the set breakpointer.

5. better rails debugger

script/breakpointer is very useful but it has a weak point as it cannot operate step execution. We understand you rarely experience the need for step execution, however, it's better to have a method than nothing. The ruby-debug Gem, informed by yugui in the last Rails meeting, will meet the need. First of all, make sure to install ruby-debug in advance.

   1  # gem install ruby-debug

Then, add a breakpoint.

   1  class FooController < ApplicationController
   2    def bar
   3      # something
   4      debugger
   5      # something
   6    end

After launching Web server such as Webrick or Mongrel on foreground, which is used in p/pp debugging, you make an access to execute targeted code through the browser. Then something similer to debug.rb launches in Web server process. Now you can start debuggin in the same manner as you do with general ruby script.

6. test/autotest

In Ruby on Rails, test framework is ready to use by default. Generally, specification is designed after writing a test code in test operation development. However, you can write a test code for verification of desired behavior and also utilize it for debugging. It takes time to operate every test by using "rake" command, I recommend to use autotest in ZenTest.This autotest operates only related tests for modified files, and can provide quicker response than using "rake" command. If you use a redgreen as a combination, the smooth operation will please you while debuggin.

7. tail -f log/development.log&; autotest

Lastly, let me intriduce my current method.I launch a separate terminal other than for development, and operate following command in RAILS_ROOT.

   1  $ tail -f log/development.log&; autotest

This arrangement enables to display the autotest output in editing program and test code and the logger output when there is an access from the browser. Basically, those two runs asynchronously, you can fully use one terminal.

posted by Png genki on Fri 4 Jan 2008 at 05:17
1st Tue

HAPPY NEW YEAR!

   1  class Happy
   2    def year!
   3      sleep 4.hours
   4    end
   5  end
   6  
   7  Happy.new.year!

I wish you a happy new year.

posted by Png genki on Tue 1 Jan 2008 at 00:00
Contents
How To Use Conditional RJS
今日学んだTips
Seven Debugging Tools of Rails
HAPPY NEW YEAR!
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ