Rails3でYAMLファイルのtメソッド呼出しでエラーが発生する理由

このQ&Aのポイント
  • Rails3でのYAMLファイルのtメソッド呼出しでエラーが発生しています。
  • config\locales\translation_ja.ymlファイルの形式が正しくないため、エラーが発生しています。
  • translation_ja.ymlファイルを正しい形式に修正してください。
回答を見る
  • ベストアンサー

Rails3でのYAMLファイルのtメソッド呼出し

config\locales\translation_ja.ymlファイルを下記のように編集して、 ja: activerecord: models: demandapp: "要望" #g attributes: demandapp: acquisition: "補足" #g class1: "大分類" #g class2: "分類" #g content: "内容" #g derivery: "納期" #g report_count: "件数 #g" source: "提起" #g state: "状態" #g これをviewのindexから下記のように、I18n.tメソッドを使って日本語訳を呼び出そうとすると、 <% Demandapp.column_names.each do |col| %> <th> <%= t("activerecord.attributes.demandapp.#{col}") %> # <ーここ </th> <% end %> I18n::InvalidLocaleData in Demandapps#index can not load translations from c:/user/Ruby193/rails/demand/config/locales/translation_ja.yml, expected it to return a hash, but does not #の所でエラーが出て理由が分かりかねています。 ご経験のある方、御教示願えればと思います。

  • Ruby
  • 回答数1
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • tatsu99
  • ベストアンサー率52% (391/751)
回答No.1

translation_ja.ymlの記述に問題があります。 ymlはインデントが命です。インデントは半角の空白のみ使用可能です。 タブ、全角空白を、使用すると正しく動作しません。以下のように記述してください。 --------------------------------------- ja:   activerecord:     models:       demandapp: "要望" #g     attributes:       demandapp:         acquisition: "補足" #g         class1: "大分類" #g         class2: "分類" #g         content: "内容" #g         derivery: "納期" #g         report_count: "件数"  #g         source: "提起" #g         state: "状態" #g ---------------------------------------- 尚、このgooのサイトは半角のインデントは使えないので、全角で投稿しています。 あなたが、この記述をするときは、必ず半角の空白で記述してください。 又、 report_count: "件数 #g" ですが これは report_count: "件数" #g の誤りと理解しました。 参考までに index.html.erbの内容と実行結果です。 ------------------------ <h1>Listing demandapps</h1> <table> <tr> <th>Acquisition</th> <th>Class1</th> <th>Class2</th> <th>Content</th> <th>Derivery</th> <th>Derivery</th> <th>Report count</th> <th>Source</th> <th>State</th> <th></th> <th></th> <th></th> </tr> <% @demandapps.each do |demandapp| %> <tr> <td><%= demandapp.acquisition %></td> <td><%= demandapp.class1 %></td> <td><%= demandapp.class2 %></td> <td><%= demandapp.content %></td> <td><%= demandapp.derivery %></td> <td><%= demandapp.derivery %></td> <td><%= demandapp.report_count %></td> <td><%= demandapp.source %></td> <td><%= demandapp.state %></td> <td><%= link_to 'Show', demandapp %></td> <td><%= link_to 'Edit', edit_demandapp_path(demandapp) %></td> <td><%= link_to 'Destroy', demandapp, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </table> <br /> <% Demandapp.column_names.each do |col| %> <th> <%= t("activerecord.attributes.demandapp.#{col}") %> <%#= Demandapp.human_attribute_name("#{col}") %> </th> <% end %> <br /> <%= link_to 'New Demandapp', new_demandapp_path %> ------------------------

tmiyoshi
質問者

お礼

これは、一か所追加した記述の所で、TABキーを使っているのが原因でした。 情報、ありがとうございました。

関連するQ&A

  • CSSのテーブルについて教えて下さい。

    テーブルなのですがh1が適用されません。 又表の枠線が黒になりません。 また(休診)を赤にしたいのですが表示されません。 どこをどう直したらいいのか教えて下さい。 お願いします。 h1 { margin: 1em 0; padding: 0.3em; border: 1px silver solid; background: url(../image/subpage_h2_bg.gif) bottom repeat-x; color: gray; font-weight: bold; font-size: large; } /*hyou*/ caption{ color: #996666; padding-bottom: 14px; text-align: center; } table{ width: 800px; border: solid 2px ; border-collapse: collapse; } th,td { padding: 5px; border: solid 1px #000000; text-align:center; } th { background-color:silver; font-weight: bold; } td.col01{ font-weight: bold; background-color:silver; } em { color: red; font-weight: bold; } /* テーブル列幅指定 */ .cola { width:100px; } .col01{ width: 100px; } .col02{ width: 70px; } .col03{ width: 70px; } .col04{ width: 70px; } .col05{ width: 70px; } .clo06{ width: 70px; } .style1 {margin-left:1em; } HTML <h1>外来診療のご案内</h1> <table> <caption>外来診療 予定表</caption> <tr> <th class="a">診察科</th> <th class="b">月</th> <th class="c">火</th> <th class="d">水</th> <th class="e">木</th> <th class="f">金</th> <th class="g">土</th> <th class="h">日</th> </tr> <tr> <th class="col01">一般歯科</th> <td class="col02" rowspan="4"><em>休診</em></td> <td class="col03" colspan="5">◯</td> <td class="col04" rowspan="4"><em>休診</em></td> </tr> <tr> <th class="col01">小児歯科</th> <td class="col02">◯</td> <td class="col03">&nbsp;</td> <td class="col04">◯</td> <td class="col05">&nbsp;</td> <td class="col06">◯</td> </tr> <tr> <th class="col01">歯列矯正</th> <td class="col02">&nbsp;</td> <td class="col03">◯</td> <td class="col04">&nbsp;</td> <td class="col05">◯</td> <td class="col06">&nbsp;</td> </tr> <tr> <th class="col01">審美歯科</th> <td class="col02">&nbsp;</td> <td class="col03">◯</td> <td class="col04">&nbsp;</td> <td class="col05">&nbsp;</td> <td class="col06">◯</td> </tr> </table>

    • ベストアンサー
    • HTML
  • よくわかるPHPの教科書のエラー?について

    わからないところがあるので教えて下さい。 (コード) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. w3.org/TR/xhtmll/DTD/xhtmll- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>PHP入門</title> </head> <body> <?php require('dbconnect.php'); $page = $_REQUEST['page']; if($page =='') { $page =1; } $page = max($page, 1); //最終ページを取得する $sql ='SELECT COUNT(*) AS cnt FROM my_items'; $recordSet = mysqli_query($db, $sql); $table = mysqli_fetch_assoc($recordSet); $maxPage = ceil($table['cnt'] /5); $page = min($page, $maxPage); $start = ($page - 1) * 5; $recordSet = mysqli_query($db, 'SELECT m.name, i.* FROM makers m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC LIMIT ' . $start . ',5'); ?> <p><a href="input.php">新しい商品を登録する。</a></p> <table width="100%"> <tr> <th scope="col">ID</th> <th scope="col">メーカー</th> <th scope="col">商品名</th> <th scope="col">価格</th> <th scope="col">編集・削除</th> </tr> <?php while($table = mysqli_fetch_assoc($recordSet)) { ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></td> <td><?php print(htmlspecialchars($table['name'])); ?></td> <td><?php print(htmlspecialchars($table['item_name'])); ?></td> <td><?php print(htmlspecialchars($table['price'])); ?></td> <td><a href="update.php?id=<?php print(htmlspecialchars($table['id'])); ?>">編集</a> <a href="delete.php?id=<?php print(htmlspecialchars($table['id'])); ?>" onclick="return confirm('削除してもよろしいですか?');">削除</a> </td> </tr> <?php } ?> </table> <ul class="paging"> <?php if($page > 1) { ?> <li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ </a></li> <?php } else { ?> <li>前のページへ</li> <?php } ?> <?php if($page < $maxPage) { ?> <li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ </a></li> <?php } else { ?> <li>次のページへ</li> <?php } ?> </ul> </body> </html> 参考書?はよくわかるPHPの教科書5.5対応なのですが、一番初めにこのファイルを開くと Notice: Undefined index: page in C:\xampp\htdocs\shop\index.php on line 14っとエラー?が出ます。 次のページ、前のページを押すとエラー?は消えるのですが、とっても気になります。 なるべくなら、エラーを非表示にせずにプログラム内で問題を解決したいのですが・・・ 初心者なのでどうして良いかわかりません。 出来れば、とってもわかりやすい解決を出来ればお願いしたいです。(馬鹿なので・・・) よろしくお願いします。

    • ベストアンサー
    • PHP
  • Ruby on Rails について

    Ruby on Railsでアプリケーションを作ります。 バージョンは:InstantRails-2.0-win 1、データベース表の作成: データベース名:myapp   テーブル名:peoples データベースを作成したら、MySQLを再起動します。 2、新規アプリケーションの作成: アプリケーション名:myapp Instant RailsウィンドウのIボタン⇒Rails Applications⇒Manage Rails Applicationsをクリックします。 Rails ApplicationsダイアログボックスのCreate New Rails App・・をクリックします。 コマンドプロンプト画面が表示されます。 rails myapp を入力します。 コマンドプロンプトのプロンプトが入力状態になって、コマンドプロンプトを閉じます。 Rails ApplicationsダイアログボックスのRefresh Listボタンをクリックしたら、myappアプリケーション名が追加されます。 3、アプリケーション・モデルの作成: InstantRails-2.0-winフォルダ ⇒ rails_apps ⇒ myapp ⇒ config ⇒ datebase.yml ファイルを開きます。 ↓に書き換えます。 development: adapter: mysql database: myapp timeout: 5000 username: root host: localhost   Instant RailsウィンドウのIボタン ⇒ Rails Applications ⇒ Open Ruby Console Windowをクリックします。 コマンドプロンプトが開きます。 cd myapp ruby script/generate model people を入力します。 4、コントローラの作成: コマンドプロンプトを開きます。 cd myapp ruby script/generate controller people を入力します。 コマンドプロンプトを閉じます。 InstantRails-2.0-winフォルダ ⇒ rails_apps ⇒ myapp ⇒ app ⇒ controllers ⇒ people_controller.rb ファイルを開きます。 class PeopleController<Application Controller scaffold :people ←←追加した行です。 end 5、新しいアプリケーションを起動 Rails ApplicationsダイアログボックスのRails Applications 欄の myappの所をチェックし、Start with Mongrelボタンをクリックします。 コマンドプロンプト画面が起動し、ポート番号が表示されます。 Webブラウザを起動し、アドレス欄に 一つは: 「http://localhost:people/list」を入力し、Enterキーを押します。 ↓Errorメッセージが表示されました。 NoMethodError in PeopleController#list undefined method `scaffold' for PeopleController:Class RAILS_ROOT: C:/taiyo/INSTAN~1.0-W/rails_apps/myapp   一つは: http://localhost:3000/people Enterキーを押します。 ↓Errorメッセージが表示されました。 NoMethodError in PeopleController#index undefined method `scaffold' for PeopleController:Class RAILS_ROOT: C:/taiyo/INSTAN~1.0-W/rails_apps/myapp どこの手順が違ってるのが解らなくて、 Errorの解決方法ですごく困ってます。 どうかご指導をお願いいたします。 もう一つはErrorメッセージの3行目は C:/taiyo/INSTAN~1.0-W/rails_apps/myappですけど、 InstantRails-2.0-winフォルダのはずですけど、 INSTAN~1.0-Wフォルダになった原因がよくわからないです。 すごく困ってます。 ご指導お願いいたします。

  • PHPとMySQL 検索フォームを作りたい。

    PHPとMySQLを使って商品一覧を作りました。 ◆商品一覧表 http://pips.chu.jp/0/07/index.php <?php function connect() { return new PDO("mysql:dbname=★.★","★","★", array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")); } $pdo = connect(); $st = $pdo->query("SELECT * FROM shohin"); $shohin = $st->fetchAll(); ?>    <TABLE border="1"> <tr> <th>商品番号</th> <th>商品名</th> <th>分類</th> <th>販売単価</th> <th>仕入単価</th> <th>登録日</th> </tr> <?php foreach ($shohin as $g) { ?> <tr> <td><?php echo $g['id'] ?></td> <td><?php echo $g['name'] ?></td> <td><?php echo $g['bunrui'] ?></td> <td class="td-price"><?php echo number_format ($g['htanka']) ?></td> <td class="td-price"><?php echo number_format ($g['stanka'])?></td> <td><?php echo $g['day'] ?></td> </tr> <?php } ?> </TABLE> ◆特定の商品一覧表 http://pips.chu.jp/0/07/irui.php ↓ $st = $pdo->query("SELECT * FROM shohin");の部分を $st = $pdo->query("SELECT * FROM shohin WHERE bunrui='衣類' ");に変更すると 分類が衣類だけの表になります。 ◆入力フォーム http://pips.chu.jp/0/07/form.php ↓ <form action = "kensaku.php" method="post">    分類:<input type="text" name="bunrui">    <input type="submit" name="submit" value="検索"> </form> こんな感じの入力フォームを作り、分類名を入力すると、その分類名の一覧表に反映されるようにしたいです。 ◆検索された一覧表 http://pips.chu.jp/0/07/kensaku.php ↓ <?php function connect() { return new PDO("mysql:dbname=★.★","★","★", array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")); } $pdo = connect(); $st = $pdo->query("SELECT * FROM shohin WHERE bunrui='?' "); $shohin = $st->fetchAll(); ?>    <TABLE border="1"> <tr> <th>商品番号</th> <th>商品名</th> <th>分類</th> <th>販売単価</th> <th>仕入単価</th> <th>登録日</th> </tr> <?php foreach ($shohin as $g) { ?> <tr> <td><?php echo $g['id'] ?></td> <td><?php echo $g['name'] ?></td> <td><?php echo $g['bunrui'] ?></td> <td class="td-price"><?php echo number_format ($g['htanka']) ?></td> <td class="td-price"><?php echo number_format ($g['stanka'])?></td> <td><?php echo $g['day'] ?></td> </tr> <?php } ?> </TABLE> 「入力フォーム」と「検索された一覧表」のソースの書き方がわかりません。 どのようにけば実現するでしょうか。 まだ初心者なので、本やサイトを調べながら見よう見まねでやっています。 具体的に、どの部分を書き換えればよいか教えていただければ有難いです。 よろしくお願いします。

  • 「未定義の変数」の定義について(PHP)

    ●質問の主旨 添付画像の左端に Notice: Undefined index: page in C:\xampp\htdocs\shop\index.php on line 3 ということで、未定義の変数ということでおしらせが出ています。 この表示を消すためにはどうすれば良いでしょうか? ご存知のかた、ご教示願います。 ●質問の補足 下記のコードで言えば、 $page =$_REQUEST['page']; のうち、pageが定義されていないことが、表示の原因である気がします。 しかし、どこをどう書き換えれば、表示が消えるのかが分かりません。 ●参考文献 たにぐちまこと「よくわかるPHPの教科書」(P210)の index.phpファイル ●開発環境 windows8 xammp1.8.1 ●コード <?php require('dbconnect.php'); $page =$_REQUEST['page']; if ($page == '') { $page = 1; } $page = max($page, 1); //最終ページを取得する $sql = 'SELECT COUNT(*) AS cnt FROM my_items'; $recordSet = mysql_query($sql); $table = mysql_fetch_assoc($recordSet); $maxPage = ceil($table['cnt'] / 5); $page = min($page, $maxPage); $start = ($page - 1) * 5; $recordSet = mysql_query('SELECT m.name, i. * FROM makers m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC LIMIT ' . $start .',5'); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="style.css" /> <title>Webシステムをつくる</title> </head> <body> <div id="wrap"> <div id="head"> <h1>トップページ</h1> </div> <div id="content"> <p style="margin-top: 20px"> <table width="100%"> <tr> <th scope="col">ID</th> <th scope="col">メーカー</th> <th scope="col">商品名</th> <th scope="col">価格</th> </tr> <?php while ($table = mysql_fetch_assoc($recordSet)) { ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></td> <td><?php print(htmlspecialchars($table['name'])); ?></td> <td><?php print(htmlspecialchars($table['item_name'])); ?></td> <td><?php print(htmlspecialchars($table['price'])); ?></td> </tr> <?php } ?> </table> <ul class="paging"> <?php if ($page > 1) { ?> <li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ</a></li> <?php } else { ?> <li>前のページへ</li> <?php } ?> <?php if ($page < $maxPage) { ?> <li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ</a></li> <?php } else { ?> <li>次のページへ</li> <?php } ?> </ul> </p> </div> <div id="foot"> <p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p> </div> </div> </body> </html>

    • ベストアンサー
    • PHP
  • onloadでのメソッド呼び出しについて

    HTMLファイルのbody部分で <body onload="start('')">のようにして、JavaScriptのメソッドを呼び出しています。 しかし、このやり方はよくないと言われてしまいまして、ではどうすればいいのかと いうところを相手方に聞けず…。 bodyに書くのが規約的によくないみたいでしたので、bodyに書く以外で、ページを 表示した際にjavaScriptのメソッドが呼べればいいんですが、他のやり方を教えて いただけないでしょうか。 宜しくお願いします。

  • [PowerShell] メソッド内からの呼び出し

    お世話になります。 Win10、PowerShellでアプリの作成を行っています。 アセンブリ追加したクラスを使用したいのですが、Add-Typeで追加したクラスを使用することが出来ず困っております。 ■成功例 Add-Type -Assembly System.Windows.Forms [System.Windows.Forms.MessageBox]::Show("めっせーじ!") # → これだと期待通りにメッセージダイアログが表示されます。 ■失敗例 Add-Type -Assembly System.Windows.Forms class ClsTest {  exec() {   [System.Windows.Forms.MessageBox]::Show("めっせーじ!")  } } $obj = New-Object ClsTest $obj.exec() # → クラスのメソッドに入れた途端、[System.Windows.Forms.MessageBox]が見つかりません、と言うエラーが発生します メソッドの中から期待通りに呼び出すには、どうすればよろしいでしょうか。 なお、当該部分を内包したグローバルなファンクションを別途用意し、それをメソッドから呼び出す方法は無しでお願いします。 うまく行くことは確認していますが、実際にはもっと複雑な処理を複数のクラスを使って行いたいため、応用できませんでした。 以上、なにとぞご教示のほどよろしくお願いいたします。

  • メソッドの再帰呼び出しについて

    再帰処理に関する質問をさせていただきます。私は今複数のarraylistの中に入っているオブジェクトを順列を用いてすべての並びを所得するプログラムを作っています。プログラムが長く汚いので(汗)、例で示させていただきます。 (例) ArrayList t1; ArrayList t2; t1の中身[obj1,obj2] t2の中身[obj3,obj4] 結果:以下のように4つオブジェクトを作成 [obj1,obj3],[obj1,obj4],[obj2,obj3],[obj2,obj4] それに伴い、メソッドを作り再帰呼び出しを行っているんですが、returnでメインに返りません。戻り値はvoidです。また、 return; System.out.println("check"); と書くと、checkが出力されてしまっています(しかも何個も)。IndexOutOfBoundsExceptionのエラーがでるのですが、先にreturnを処理するはずなのになぜだろうと悩んでいます。 return以外にメインに戻る方法はあるんでしょうか?またreturnでメインに戻らない理由を知っている方がいるなら、ぜひ教えていただきたいです。よろしくお願いします。

  • Javaのメソッド呼び出しについて

    void usage() { System.out.println("zzz"); System.out.println("xxx"); System.out.println("ccc"); } void finish() { System.out.println("aaa"); } 上記の2つのメソッドを、mainメソッドから呼び出したいのですが どうやるのでしょうか? かなり足りない部分があるかと思うんですが、 おしえていただけないですか?

    • ベストアンサー
    • Java
  • javaのメソッド呼び出し

    javaで質問があります。メソッドを呼び出すとき、 クラス.メソッド(引数)だと思いますが、 「.」が2つあるものがあります。 どういったものでしょうか? あるプログラムに Code.PerComFlag.ReCom.equals(atmlogDTO.getatmComFlg()) のがありました。 また、javaの本に System.out.println(・・・) という記述もあります。 よろしくお願いします。

    • ベストアンサー
    • Java