0

ちょっとこれはおそらく非常に簡単な質問ですが、show.html.erb ページを表示できないようで、私は本当の初心者です。これは ruby​​ on rails で作成されています。これが私のコードです:

Post.rb

class Post < ActiveRecord::Base
  attr_accessible :artist
  has_many :songs
end

ソング.rb

class Song < ActiveRecord::Base
  attr_accessible :lyric, :name, :song_id
  belongs_to :post
end

CreatePost Migrate

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :artist
      t.string :song

      t.timestamps
    end
  end
end

CreateSong 移行

class CreateSongs < ActiveRecord::Migration
  def change
    create_table :songs do |t|
      t.text :lyric
      t.string :name
      t.integer :song_id

      t.timestamps
    end
  end
end

show.html.erb

<center><h1><%= @post %></h1></center>
<p> Songs: </p>
<% @post.song.song_id.each do |s| %>
    <p>
        <%= s.name %>
    </p>
<% end %>
<% form_for [@post, Song.new] do |f| %>
  <p>

    <%= f.label :name, "Artist" %><br />
    <%= f.text_field :name %><br />
    <%= f.label :body, "Song Name:" %><br />
    <%= f.text_field :body %>
  </p>

  <p>
    <%= f.submit "Add Song" %>
  </p>
<% end %>
4

1 に答える 1

0

コントローラーが足りないようです。

Ruby on Rails のコントローラーは、モデルとビューの間でデータをマーシャリングします。

app/controllers フォルダーに次のようなコントローラーを作成することをお勧めします。

class PostsController < ApplicationController

  def show
    @post = Post.find(params[:id])
  end
end

また、ルート ファイルで posts リソースが設定されていることを確認する必要があります。

config/routes.rb ファイルに置くことができます

resources :posts

これにより、/posts URL が posts コントローラーを参照していることを Rails に伝えます。

RoR を使い始めるのは、さまざまな可動部分があるため少し難しい場合があります。短いチュートリアル スニペットについては、 railscasts.comを強くお勧めします。より詳細な手順については、 peepcode.comも参照してください。

于 2013-05-06T22:02:43.350 に答える