• 締切済み

ruby on railsのコントローラー内でputsが動作しない

タイトルのままなのですが、railsを現在勉強中です。 簡単なデバッグでsample_controller.rb内でputsやprintで変数を表示させようとしてるのですが、まったく表示されません。 なぜこのような現象がおこるのでしょう? htmlの表示はくずれるもののブラウザに表示されるかと思ったのですが。 「テンプレート使えばいい」というのはごもっともなのですが、疑問解消にお力添えいただければ幸いです。

みんなの回答

  • asuncion
  • ベストアンサー率33% (2126/6288)
回答No.1

> 簡単なデバッグでsample_controller.rb内でputsやprintで変数を表示させようとしてるのですが、まったく表示されません。 中身を見せていただくことは可能ですか?

eltech
質問者

補足

返信、ありがとうございます。 下記がソースとなります。 listメソッドに該当する箇所があります。 class AdminController < ApplicationController def index list render :action => 'list' end # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list } def list puts "test" @article_pages, @articles = paginate :articles, :per_page => 10 end def show @article = Article.find(params[:id]) end def new @article = Article.new end def create @article = Article.new(params[:article]) if @article.save flash[:notice] = 'Article was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end def edit @article = Article.find(params[:id]) end def update @article = Article.find(params[:id]) if @article.update_attributes(params[:article]) flash[:notice] = 'Article was successfully updated.' redirect_to :action => 'show', :id => @article else render :action => 'edit' end end def destroy Article.find(params[:id]).destroy redirect_to :action => 'list' end end

関連するQ&A

  • 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フォルダになった原因がよくわからないです。 すごく困ってます。 ご指導お願いいたします。

  • ruby on rails チュートリアル

    チュートリアルの3章を勉強中なのですが(http://railstutorial.jp/chapters/static_pages?version=4.2#sec-generated_static_pages) リスト3-4で $ rails generate controller StaticPages home help このコマンドをしてから$ rails server -b $IP -p $PORTを入力すると => Booting WEBrick => Rails 4.2.4 application starting in development on http://0.0.0.0:8080 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2015-11-11 09:39:15] INFO WEBrick 1.3.1 [2015-11-11 09:39:15] INFO ruby 2.2.1 (2015-02-26) [x86_64-linux] Exiting /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/socket.rb:206:in `bind': Address already in use - bind(2) for 0.0.0.0:8080 (Errno::EADDRINUSE) from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/socket.rb:206:in `listen' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/socket.rb:461:in `block in tcp_server_sockets' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/socket.rb:232:in `each' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/socket.rb:232:in `foreach' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/socket.rb:459:in `tcp_server_sockets' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/utils.rb:70:in `create_listeners' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:133:in `listen' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:114:in `initialize' from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:45:in `initialize' from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:31:in `new' from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:31:in `run' from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:286:in `start' from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/commands/server.rb:80:in `start' from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:80:in `block in server' from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `tap' from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `server' from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>' from /home/ubuntu/workspace/sample_app/bin/rails:8:in `require' from /home/ubuntu/workspace/sample_app/bin/rails:8:in `<top (required)>' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `load' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `call' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client.rb:26:in `run' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/bin/spring:48:in `<top (required)>' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load' from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>' from /home/ubuntu/workspace/sample_app/bin/spring:13:in `require' from /home/ubuntu/workspace/sample_app/bin/spring:13:in `<top (required)>' from bin/rails:3:in `load' from bin/rails:3:in `<main>' となってしまいますがこれってエラーですよね? ビューも開けないのですが、どなたか教えていただきたいです もしかしたら上のコマンド関係ないのかもしれないです。 よろしくお願いします。

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

    Ruby on RailsでWebアプリケーションを作成します。 下記の順でやりました。 1、データベース表を作成。 2、新規アプリケーションの作成。 3、作成したアプリケーションがどのデータベースを使うのかを教える。 4、モデルの作成。 5、コントローラの作成。 6、新しいアプリケーションを起動。 ここでコマンドプロンプト画面内に表示されるポート番号を確認し、 Webブラウザを起動し、 アドレス欄に[http://localhost:ポート番号/アドレスブック/アドレスブックのリスト] を記入しました。 エラーが表示されました。 NoMethodError in PeopleController#list undefined method `scaffold' for PeopleController:Class

  • ruby on railsの シンタックスエラー

    ruby on rails の初心者です。 rubyのバージョンは、ruby 2.0.0p451 (2014-02-24 revision 45167)です。 http://www.rubylife.jp/rails/ini/index4.html の初心者向けの記事を読みながら、コントローラーとアクションの設定の勉強をしていたのですが、 終盤どうしてもrails sでサーバーを起動すると、 /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.1/lib/action_dispatch/routing/mapper.rb:196:in `normalize_conditions!': You should not use the `match` method in your router without specifying an HTTP method. (RuntimeError) If you want to expose your action to both GET and POST, add `via: [:get, :post]` option. If you want to expose your action to GET, use `get` in the router: Instead of: match "controller#action" Do: get "controller#action” という表示がでてきてしまい起動できません。 matchをgetなどにかえて対処使用としても無理でした(sintax errolorがでます)。 どうしてでしょうか。どなたか教えて頂けると幸いでございます。

  • Ruby on Rails によるデータベース作成

    Rubyもよくわからない初心者ですが、「かんたんRuby on RailsでWebアプリケーション開発」という本を見ながらデータベースを作成しています。Railsのバージョンは1.1.2、データベース管理はMySQLを使っています。 マイグレーションの作成まではうまく行っていると思うのですが、001_create_table.rbの修正、database.ymlの設定を行い、マイグレーション、scaffoldを実行してブラウザで表示すると、表示されないフィールドがあります。 001_create_table.rbでは次の部分を追加しています。 create_table(:items) do |table| table.column :index, :string, :limit => 32 table.column :pos, :string, :limit => 8 table.column :cid, :string, :limit => 32 table.column :type, :string, :limit => 5 table.column :flq, :integer table.column :ns, :string, :limit => 6 table.column :cflg, :integer table.column :wflg, :integer end 何回かやり直してみましたが、↑この中のtypeというフィールドがどうしても表示されません。 どこを直したらいいのか教えてください。 よろしくお願いします。

  • Rails2.3でのserver/script再起動について

    Rails2.3でのserver/script再起動について 2.2.2を利用していたのですが、2.3.5へアップデートしようとしています。 rails 2.3.5のインストールは無事、完了し、プロジェクトを作成しました。 controller名を「hoge」にて、server/scriptにてwebrickを起動しました。 次にcontrollerにて、indexアクションを作成し、viewにて対応するindex.html.erbを作成しました。 ブラウザにて、http://localhost:3000/hoge/indexにアクセスすると、 「Template is missing」となってしまいます。 webrickを再起動し、再度アクセスすると正常に表示されます。 rails 2.2.2までは、development環境下では、 viewのファイル追加では再起動が不要だった(config以下は必要だった)と認識しているのですが、 rails 2.3では再起動が必要になったのでしょうか?ちなみにmongrel 1.1.5で行っても同じでした。 どなたかご教授お願いいたします。

  • Rails3でのルーティングの書き方

    Ruby1.9.3とRails3.2.9の環境でのルーティングではまってしまっているので、御経験のある方、御教示ください。現在、 config/routes.rbに Sample::Application.routes.draw do resources :users resources :tasks match '/users/login' match '/users/logout' match ':controller(/:action(/:id))' match ':controller(/:action(/:id))(.:format)' end を記述して、WEBrickを起動して、 http://localhost:3000/users/login/ を実行すると、このアドレスには移動するのですが、 NoMethodError in Users#show Showing C:/user/Ruby193/rails/sample/app/views/users/show.html.erb where line #5 raised: のエラーが出ます。 WEBrickの表示を見てみると、 Started GET "/users/login/" for 127.0.0.1 at 2013-01-22 22:56:06 +0900 Processing by UsersController#show as HTML Parameters: {"id"=>"login"} Rendered users/show.html.erb within layouts/users (2.0ms) Completed 500 Internal Server Error in 5ms となっていて、/users/login/をGETしているのですが、アクションでshowが呼び出されて、そのIDとして loginが渡っているように見えます。 現在、users\controller.rbのshowの関数の定義は全てコメントアウトしています。usersコントローラの def login関数の定義と、app/views/users/login.html.erbも置いているのですが、それらが表示されない状態で原因が分かりかねています。 上記のルーティングの書き方に何か問題があるのでしょうか?

    • ベストアンサー
    • Ruby
  • Rails deviseログイン画面について

    deviseをインストールしました。 全ページに認証がかかるように、application_controller.rbの最後に以下を追記しました。 before_action :authenticate_user! すると、表示させたいログイン画面とは別のログイン画面があらわれたのですが、 なぜでしょうか? URLを見ると「http://localhost:3000/users/sign_in」になっています。 どなたか、詳しい方がいらっしゃいましたら、ご教授をお願いします。 【環境】 Ruby 2.2.2 Rails 4.0.13 CentOS6 devise 3.5.1 以上、宜しくお願いします。

  • Rubyでの配列データの表示

    NetBeansを使ってRubyの勉強をしています。 PHPのprint_rのように配列のデータ構造などを画面に表示させたいのですが、 Rubyの場合は、pやppというのを使うと検索して知りました。 サイトの説明の通りに、test.rbというコントローラとtest.rhtmlというビューを用意して test.rbに以下のコードを記述したのですが、表示されません。 def test require 'pp' profile = {"last_name" => "yamada", 'first_name' => "takesi", 'addres' => 'Tokyo', 'old' => 28, 'height' => 172, "weight" => 65} p profile print "\n" pp profile end どうすれば、配列データをブラウザで表示することができるのでしょうか。 どうかご教示いただけますよう宜しくお願い致します。

  • rails3初心者 年齢を表示する

    rails3を勉強中です。 お詳しい方、ご教授いただけたら嬉しいです。 実装したい内容 プルボンタンで年齢「20」が選ばれた場合、 その結果を出力する(非モデル) ==================== views:presons/index.html ==================== <%= form_tag :action => :age do %> <%= select("form","age", (1..100))%> <%= submit_tag "表示する"%> <% end %> ==================== presons_controller.rb ==================== def index @preson = Preson.new end def age @preson = params[:form][:age] redirect_to :action => 'index' end ==================== views:presons/index.htmlで選択された 「20」を表示したいのですが、 そのコードの書き方をご教授ください。 どうぞよろしくお願い致します。

    • ベストアンサー
    • Ruby