1

在这里,您会找到 Ryan 的名为Mongoid(修订版)的 railscast 。

我在 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个模型。产品.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/reviews/index.html.erb上显示

4

1 回答 1

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 />

现在它在单独的页面上显示评论!:)

于 2013-06-07T17:39:13.360 回答