在这里 ,你会发现瑞安的railscast称为Mongoid(修订)。
我创建了railscast下一个应用程序,并把它放在https://github.com/tenzan/store.git
在这个例子中, 产品集合已经嵌入了评论 。
我能够从内轨道控制台添加评论,想显示在浏览器上,我想不通。
我觉得我有在扭捏
app/controllers/reviews_controller.rb
和
app/views/reviews/index.html.erb
该应用程序创建的Rails 3和Ruby 1.9.3p429。
编辑:
我有2种型号。 product.rb
class Product
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :price, type: BigDecimal
field :released_on, type: Date
attr_accessible :name, :price, :released_on
validates_presence_of :name
embeds_many :reviews
accepts_nested_attributes_for :reviews
end
和review.rb
class Review
include Mongoid::Document
field :content, type: String
embedded_in :product
end
我在产品集合一个文件:
{
"_id" : ObjectId("51b1eac0311b6dd93a000001"),
"created_at" : ISODate("2013-06-07T14:14:24.714Z"),
"name" : "Apple",
"price" : "34.45",
"released_on" : ISODate("2013-06-06T00:00:00Z"),
"reviews" : [
{
"_id" : ObjectId("51b1ec2b311b6db065000001"),
"content" : "greate game!"
},
{
"_id" : ObjectId("51b1ec56311b6db065000002"),
"content" : "cool game!"
}
],
"updated_at" : ISODate("2013-06-07T14:14:24.714Z")
}
我想访问的单独的页面上的产品评论,所以我把一个链接在应用程序/视图/产品/ show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @product.name %>
</p>
<p>
<b>Price:</b>
<%= @product.price %>
</p>
<p>
<b>Released on:</b>
<%= @product.released_on %>
</p>
<table border = 1>
<tr>
<th>Reviews</th>
</tr>
<% @product.reviews.each do |review| %>
<tr>
<td><%= review.content %></td>
</tr>
<% end %>
</table>
<%= link_to "Reviews", product_reviews_path(@product) %>
<br />
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
耙路线 :
product_reviews GET /products/:product_id/reviews(.:format) reviews#index
POST /products/:product_id/reviews(.:format) reviews#create
new_product_review GET /products/:product_id/reviews/new(.:format) reviews#new
edit_product_review GET /products/:product_id/reviews/:id/edit(.:format) reviews#edit
product_review GET /products/:product_id/reviews/:id(.:format) reviews#show
PUT /products/:product_id/reviews/:id(.:format) reviews#update
DELETE /products/:product_id/reviews/:id(.:format) reviews#destroy
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
我认为,当我点击来自应用程序/视图/产品/ show.html.erb链接:
<%= link_to "Reviews", product_reviews_path(@product) %>
它会带我到应用程序/视图/评论/ index.html.erb:
<h1>Reviews</h1>
<% @product = Product.find(params[:product_id]) %>
<table>
<tr>
<th>Content</th>
</tr>
<% @product.reviews.each do |review| %>
<tr>
<td><%= review.content %></td>
</tr>
<% end %>
</table>
当它带我到应用程序/意见/评论/ index.html.erb,我得到了错误:
NoMethodError in ReviewsController#index
undefined method `reviews' for nil:NilClass
Rails.root: /Users/askar/Dropbox/rails_studio/store
Application Trace | Framework Trace | Full Trace
app/controllers/reviews_controller.rb:4:in `index'
Request
Parameters:
{"product_id"=>"51b1eac0311b6dd93a000001"}
我的应用程序/控制器/ reviews_controller.rb如下:
class ReviewsController < ApplicationController
def index
@reviews = @products.reviews
end
end
我相信我做一个简单的错误。
顺便说一句,我是能够显示嵌入在应用程序/视图/产品/ show.html.erb文件的评论,但我想显示在应用程序/意见/评论/ index.html.erb