Here你会发现Ryan的轨道广播称为 Mongoid(修订版)。
我在railscast后创建了一个应用并将其放在https://github.com/tenzan/store.git
上在此示例中,产品集合已嵌入评论。
我能够在rails控制台中添加评论,并希望在浏览器上显示,这是我无法弄清楚的。
我觉得我必须在
中进行调整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")
}
我想在单独的页面上访问产品评论,因此我在 app / views / products / 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
我假设,当我点击 app / views / products / show.html.erb 中的链接时:
<%= link_to "Reviews", product_reviews_path(@product) %>
它会带我去 app / views / reviews / 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>
当我带到 app / views / reviews / 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"}
我的 app / controllers / reviews_controller.rb 如下:
class ReviewsController < ApplicationController
def index
@reviews = @products.reviews
end
end
我相信我犯了一个小错误。
顺便说一句,我能够在 app / views / products / show.html.erb 上显示评论嵌入式文档,但我想在 app / views /上显示评论/ index.html.erb
答案 0 :(得分:1)
解决。 Aash Dhariya帮助在mongoid google群体中解决了这个问题。
我确实更改了 reviews_controller.rb :
class ReviewsController < ApplicationController
def index
@product = Product.find(params[:product_id])
@reviews = @product.reviews
end
end
和 app / views / reviews / index.html.erb :
<h1>Reviews</h1>
<% @product = Product.find(params[:product_id]) %>
<table>
<tr>
<th>Content</th>
</tr>
<% @reviews.each do |review| %>
<tr>
<td><%= review.content %></td>
</tr>
<% end %>
</table>
<br />
现在它在单独的页面上显示评论! :)