使用Mongoid访问Rails 3上的子文档 [英] accessing sub documents on Rails 3 using Mongoid

查看:72
本文介绍了使用Mongoid访问Rails 3上的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处,您会发现Ryan的轨道广播 Mongoid(已修订) strong>.

Here you will find Ryan's railscast called Mongoid (revised).

我已经按照railscast创建了一个应用程序,并将其放在 https://github.com/tenzan /store.git

I've created an app following the railscast and put it on https://github.com/tenzan/store.git

在此示例中,产品集合已嵌入评论.

In this example, product collection has embedded reviews.

我能够在Rails控制台中添加评论,并希望在浏览器中显示我不知道的内容.

I was able to add reviews within from rails console and wanted to display on the browser, what I can't figure out.

我觉得我必须调整

app/controllers/reviews_controller.rb

app/views/reviews/index.html.erb

该应用是由Rails 3和ruby 1.9.3p429创建的.

The app was created Rails 3 and ruby 1.9.3p429.

我有2个模型. product.rb

I've 2 models. 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

and review.rb

and review.rb

class Review
  include Mongoid::Document
  field :content, type: String

  embedded_in :product
end

我在产品集中有一个文档:

I have one document in the products collection:

{
    "_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 上放置了一个链接:

I wanted to access the reviews for the products on the separate page, so I've put a link on the 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 中的链接时:

I assumed, when I click on the link from the 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 时,出现错误:

When it take me to app/views/reviews/index.html.erb, I got error:

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

我相信我犯了一个小错误.

I believe I'm doing a trivial mistake.

顺便说一句,我能够在 app/views/products/show.html.erb 上显示嵌入的评论文档,但我想在 app/views/上显示reviews/index.html.erb

By the way, I was able to display reviews embedded documents on the app/views/products/show.html.erb, but I wanted to display on the app/views/reviews/index.html.erb

推荐答案

已解决. Aash Dhariya协助Google杂种小组解决了这个问题.

Solved. Aash Dhariya helped to solve this in the mongoid google groups.

我确实更改了 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 />

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

Now it's showing the reviews on a separate page! :)

这篇关于使用Mongoid访问Rails 3上的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆