helper の使い方

View上でリンクやフォームを使うには、helper を利用して記述する。
あまり慣れていないので、最初は戸惑ったが慣れると便利です。
Link Helper

  • 普通のリンク
<?php echo link_to('Link String', 'moduleName/functionName?parameter=value') ?>
↓
<a href="/moduleName/functionName/parameter/value" title="interesting article">Link String</a>
  • 画像リンク
<?php echo link_to(image_tag('imageName'), 'moduleName/functionName') ?>
↓
<a href="/moduleName/functionName"><img src="/images/imageName" alt="imageName" /></a>
  • フォーム送信したい場合
<?php echo link_to('Link String', 'moduleName/functionName', 'post=true') ?>
↓
<a onclick="f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();return false;" href="/moduleName/functionName">Link String</a>
  • その他
<?php echo link_to('Link String', 'moduleName/functionName', 'confirm=遷移します、よろしいですか?') ?>
↓
<a onclick="return confirm('遷移しますよろしいですか?');" href="/moduleName/functionName">Link String</a>
<?php echo link_to('Link String', 'moduleName/functionName', 'popup=true') ?>
↓
<a onclick="window.open(this.href);return false;" href="/moduleName/functionName">Link String</a>
<?php echo link_to('Link String', '/moduleName/functionName', Array('popup' => Array('Window title','width=310,height=400,left=320,top=0'))) ?>
↓
<a onclick="window.open(this.href,'Window title','width=310,height=400,left=320,top=0');return false;" href="/moduleName/functionName">Link String</a>
<?php echo link_to('Link String', '/moduleName/functionName', 'confirm=送信します、よろしいですか? post=true') ?>
↓
<a onclick="if (confirm('送信します、よろしいですか?')) { f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit(); };return false;" href="/moduleName/functionName">Link String</a>
<?php echo link_to('Link String', '/moduleName/functionName', 'confirm=別ウィンドウを開きます、よろしいですか? popup=true') ?>
↓
<a onclick="if (confirm('別ウィンドウを開きます、よろしいですか?')) { window.open(this.href); };return false;" href="/moduleName/functionName">Link String</a>

Button Helper

  • 普通のボタン
<?php echo button_to('Button Value', '/moduleName/functionName') ?>
↓
<input value="Button Value" type="button" onclick="document.location.href='/moduleName/functionName';" />
  • サブミットボタン
<?php echo button_to('Button Value', '/moduleName/functionName', 'post=true') ?>
↓
<form method="post" class="button_to" action="/moduleName/functionName"><input value="Button Value" type="submit" /></form>
  • その他
<?php echo button_to('Button Valu', '/moduleName/functionName', 'confirm=別ウィンドウを開きます、よろしいですか? popup=true') ?>
↓
<input value="Button Valu" type="button" onclick="if (confirm('別ウィンドウを開きます、よろしいですか?')) {window.open('/moduleName/functionName');}" />

Mail Helper

  • 普通
<?php echo mail_to('mail Address', 'Link String') ?>
↓
<a href="mailto:mail Address'>Link String</a>
<?php echo mail_to('mail Address', 'Link String', array('encode' => true)) ?>
↓
<a href="&#109;&#x61;&#105;&#108;&#x74;&#x6f;&#58;&#x6d;&#121;">&#x63;&#111;&#110;</a>

Image Helper

Javascript Helper