0

嘿,这可能是一个非常简单的问题,但我似乎无法显示我的 show.html.erb 页面,而且我是一个真正的初学者。这是用红宝石制成的。这是我的代码:

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 迁移

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

显示.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

您还需要确保在路由文件中设置了帖子资源。

在 config/routes.rb 文件中,您可以放置

resources :posts

这告诉 Rails /posts url 引用了 posts 控制器。

对于所有不同的移动部件,开始使用 RoR 可能会有些棘手。我强烈推荐railscasts.com来获取简短的教程片段。还有peepcode.com以获得更深入的说明。

于 2013-05-06T22:02:43.350 回答