2011-12-30 9 views
0

レールで音楽を演奏するアプリを作る際に問題が発生しています。私は実際にデータベースから曲のプロパティを取得することはできません。モデルクラスの名前は「Music」です。このコードを実行しようとするとユニット化された定数ですがクラスが存在します

uninitialized constant Music::Title

::私が手にエラーがこれです

<div id="player"> 

<% @musics.each do |song| %> 


<%= song.title %> 
<div>Play sound #1: <a href="#" onclick="play('sound1');">play</a></div> 



<% end %> 
<%= initialize_sounds(sounds, debug=false) %> 

音楽クラスのプロパティは興味深い何

create_table "musics", :force => true do |t| 

    t.integer "song_id" 
    t.string "title" 
    t.string "tracklist" 
    t.string "info" 
    t.string "music_location" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

ですもし私が歌を取り出したら、私は2つのエントリーを得るでしょう「再生音#1div」のつまり、データベースには2つのエントリがあることが認識されます。モデルからプロパティを取得するのは間違っていますか?これはレンダリングされた部分を通して表示されています。私はアプリの投稿モデルと同様のことをすることができたし、うまくいきました。 @musicsというデータベースクエリを作成して曲を見つけました。私は@ musicsから@songsに変数を変更しようとしましたが、アプリが混乱しても運がない場合に備えて、試しました。また、私が歌のインデックスを見るために標準的な足場を使用する場合、それらはすべて上品に表示されます。どんな提案も大歓迎です。

---更新------ここ
がmusic.rbファイルです:

Started GET "/" for 127.0.0.1 at 2011-12-31 08:28:53 -0600 
    Processing by PagesController#_home as HTML 
    [1m[35mMusic Load (0.0ms)[0m SELECT "musics".* FROM "musics" 
    [1m[36mPost Load (0.0ms)[0m [1mSELECT "posts".* FROM "posts" [0m 
Rendered posts/_news.html.erb (24.0ms) 
Rendered pages/_home.html.erb within layouts/application (28.0ms) 
    [1m[35mCACHE (0.0ms)[0m SELECT "posts".* FROM "posts" 
Rendered posts/_news.html.erb (1.0ms) 
Rendered pages/_home.html.erb (2.0ms) 
Rendered musics/_player.html.erb (5.0ms) 
Rendered pages/_music.html.erb (11.0ms) 
Completed 500 Internal Server Error in 93ms 

ActionView::Template::Error (uninitialized constant Music::Title): 
    3: <% @musics.each do |song| %> 

    4: 

    5: 

    6: <%= song.title %> 

    7: <div>Play sound #1: <a href="#" onclick="play('sound1');">play</a></div> 

    8: 

    9: 

    app/views/musics/_player.html.erb:6:in `block in _app_views_musics__player_html_erb__288302969_49298280' 
    app/views/musics/_player.html.erb:3:in `each' 
    app/views/musics/_player.html.erb:3:in `_app_views_musics__player_html_erb__288302969_49298280' 
    app/views/pages/_music.html.erb:12:in `_app_views_pages__music_html_erb___111175075_22835100' 
    app/views/layouts/application.html.erb:48:in `_app_views_layouts_application_html_erb__149306977_49748748' 

Rendered c:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) 
Rendered c:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms) 
Rendered c:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (24.0ms) 

と完全なトレース:

class Music < ActiveRecord::Base 
    has_one :title 
    has_one :music_location 
    validates_uniqueness_of :song_id 


    def self.save(upload) 
    name = upload['datafile'] 
    directory = "public/music/" 
    # create the file path 
    path = File.join(directory, name) 
    #write the file 
    File.open(path, "wb") { |f| f.write(upload['datafile'])} 

    end 

    def self.player 
    find(:all, :conditions=> "select == true") 
    end 


end 

そしてここでは、開発ログ情報であります

activerecord (3.1.1) lib/active_record/base.rb:1341:in `compute_type' 
activerecord (3.1.1) lib/active_record/reflection.rb:173:in `klass' 
activerecord (3.1.1) lib/active_record/associations/association.rb:118:in `klass' 
activerecord (3.1.1) lib/active_record/associations/association.rb:166:in `find_target?' 
activerecord (3.1.1) lib/active_record/associations/association.rb:138:in `load_target' 
activerecord (3.1.1) lib/active_record/associations/association.rb:56:in `reload' 
activerecord (3.1.1) lib/active_record/associations/singular_association.rb:9:in `reader' 
activerecord (3.1.1) lib/active_record/associations/builder/association.rb:41:in `block in define_readers' 
app/views/musics/_player.html.erb:6:in `block in _app_views_musics__player_html_erb__288302969_39714996' 
app/views/musics/_player.html.erb:3:in `each' 
app/views/musics/_player.html.erb:3:in `_app_views_musics__player_html_erb__288302969_39714996' 
actionpack (3.1.1) lib/action_view/template.rb:144:in `block in render' 
activesupport (3.1.1) lib/active_support/notifications.rb:55:in `instrument' 
actionpack (3.1.1) lib/action_view/template.rb:142:in `render' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:256:in `render_partial' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:228:in `block (2 levels) in render' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument' 
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument' 
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument' 
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:227:in `block in render' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:219:in `render' 
actionpack (3.1.1) lib/action_view/renderer/renderer.rb:41:in `render_partial' 
actionpack (3.1.1) lib/action_view/helpers/rendering_helper.rb:27:in `render' 
app/views/pages/_music.html.erb:12:in `_app_views_pages__music_html_erb___111175075_22835100' 
actionpack (3.1.1) lib/action_view/template.rb:144:in `block in render' 
activesupport (3.1.1) lib/active_support/notifications.rb:55:in `instrument' 
actionpack (3.1.1) lib/action_view/template.rb:142:in `render' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:256:in `render_partial' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:228:in `block (2 levels) in render' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument' 
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument' 
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument' 
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:33:in `instrument' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:227:in `block in render' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats' 
actionpack (3.1.1) lib/action_view/renderer/partial_renderer.rb:219:in `render' 
actionpack (3.1.1) lib/action_view/renderer/renderer.rb:41:in `render_partial' 
actionpack (3.1.1) lib/action_view/helpers/rendering_helper.rb:27:in `render' 
app/views/layouts/application.html.erb:48:in `_app_views_layouts_application_html_erb__149306977_49748748' 
actionpack (3.1.1) lib/action_view/template.rb:144:in `block in render' 
activesupport (3.1.1) lib/active_support/notifications.rb:55:in `instrument' 
actionpack (3.1.1) lib/action_view/template.rb:142:in `render' 
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:52:in `render_with_layout' 
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:38:in `render_template' 
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:12:in `block in render' 
actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats' 
actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:9:in `render' 
actionpack (3.1.1) lib/action_view/renderer/renderer.rb:36:in `render_template' 
actionpack (3.1.1) lib/action_view/renderer/renderer.rb:17:in `render' 
actionpack (3.1.1) lib/abstract_controller/rendering.rb:120:in `_render_template' 
actionpack (3.1.1) lib/action_controller/metal/streaming.rb:250:in `_render_template' 
actionpack (3.1.1) lib/abstract_controller/rendering.rb:114:in `render_to_body' 
actionpack (3.1.1) lib/action_controller/metal/renderers.rb:30:in `render_to_body' 
actionpack (3.1.1) lib/action_controller/metal/compatibility.rb:43:in `render_to_body' 
actionpack (3.1.1) lib/abstract_controller/rendering.rb:99:in `render' 
actionpack (3.1.1) lib/action_controller/metal/rendering.rb:16:in `render' 
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render' 
activesupport (3.1.1) lib/active_support/core_ext/benchmark.rb:5:in `block in ms' 
c:/Ruby192/lib/ruby/1.9.1/benchmark.rb:310:in `realtime' 
activesupport (3.1.1) lib/active_support/core_ext/benchmark.rb:5:in `ms' 
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:40:in `block in render' 
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime' 
activerecord (3.1.1) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime' 
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:39:in `render' 
actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render' 
actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
actionpack (3.1.1) lib/abstract_controller/base.rb:167:in `process_action' 
actionpack (3.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action' 
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
activesupport (3.1.1) lib/active_support/callbacks.rb:425:in `_run__593797494__process_action__1048922870__callbacks' 
activesupport (3.1.1) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks' 
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
actionpack (3.1.1) lib/abstract_controller/callbacks.rb:17:in `process_action' 
actionpack (3.1.1) lib/action_controller/metal/rescue.rb:17:in `process_action' 
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument' 
activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument' 
activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument' 
actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action' 
actionpack (3.1.1) lib/action_controller/metal/params_wrapper.rb:201:in `process_action' 
activerecord (3.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
actionpack (3.1.1) lib/abstract_controller/base.rb:121:in `process' 
actionpack (3.1.1) lib/abstract_controller/rendering.rb:45:in `process' 
actionpack (3.1.1) lib/action_controller/metal.rb:193:in `dispatch' 
actionpack (3.1.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' 
actionpack (3.1.1) lib/action_controller/metal.rb:236:in `block in action' 
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `call' 
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `dispatch' 
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:29:in `call' 
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call' 
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize' 
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each' 
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize' 
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call' 
actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:532:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
rack (1.3.5) lib/rack/etag.rb:23:in `call' 
rack (1.3.5) lib/rack/conditionalget.rb:25:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/head.rb:14:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/flash.rb:243:in `call' 
rack (1.3.5) lib/rack/session/abstract/id.rb:195:in `context' 
rack (1.3.5) lib/rack/session/abstract/id.rb:190:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call' 
activerecord (3.1.1) lib/active_record/query_cache.rb:62:in `call' 
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks' 
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call' 
rack (1.3.5) lib/rack/sendfile.rb:101:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call' 
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call' 
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call' 
rack (1.3.5) lib/rack/runtime.rb:17:in `call' 
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
rack (1.3.5) lib/rack/lock.rb:15:in `call' 
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call' 
railties (3.1.1) lib/rails/engine.rb:456:in `call' 
railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call' 
railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call' 
rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service' 
c:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' 
c:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' 
c:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' 
+0

'@ musics'には何が設定されていますか? –

+0

'@musics = Music.all' – ejkt

+0

あなたのコントローラに' logger.info 'を追加してください。SONGS:#{@ musics.inspect} "'データは 'log/development.log'の出力に書き込まれます! – davidb

答えて

1

あなたからhas_one :titlehas_one :music_locationを削除します音楽モデル。

おそらくbelongs_to :songが必要です。

has_oneがわからないと思います。 has_one :titleと言うときは、Titlesという表があり、music_idの列があることを意味します。

song.titleを書くことができるようにするためにモデル内で何もする必要はありません。これは音楽モデルの属性に過ぎません。

+0

D'oh。ミックスのために別の設定を使用するときから、それを残しました。私はそれらを 'validates_uniqueness_of'に変更しました。大変ありがとう! – ejkt

関連する問題