LoginSignup
0
0

More than 5 years have passed since last update.

what should i actually do when ruby calls "do"???

Posted at

こんにちは。
今日もだらだらと技術をテキトーにつまんでは食いつまんでは食いしております。

enchant.jsをまあ大体習熟した(飽きたともいう)ので
今更ながらruby on rails をやりだしました。下記を参考に。
4. scaffoldを利用した開発(1) | TECHSCORE(テックスコア)

おりゃー、scaffold!(足場!)
これはすごい。

さてどんなコードが出力されるのかな?

#Controllerね。
  def index
    @items = Item.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @items }
    end
  end

はてなんじゃこりゃ。。。(´・ω・`)

respond_to do |format| とはなんぞや。。。
と思って調べたらすぐ理解できた。

do使用のサンプルコード。

["aaa ","bbb "].each do |x|
  p x # aaa bbb と表示される
end

forで書き換える。

for x in ["aaa ","bbb "]
  print x
end

だから多分respond_toってメソッドの結果が順にformatに入って
do〜endのブロックで実行されるのね。なるほどね。


Rails: How does the respond_to block work? - Stack Overflow

respond_to is a Rails helper method that is attached to the Controller class (or rather, its super class). 
It is referencing the response that will be sent to the View (which is going to the browser).

だからdo〜endでViewに送られる何かに処理を行ってるんだろう。

その辺はおいおい理解する。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0