问题
Here you will find Ryan's railscast called Mongoid (revised).
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.
I was able to add reviews within from rails console and wanted to display on the browser, what I can't figure out.
I feel like I have to tweak in the
app/controllers/reviews_controller.rb
and
app/views/reviews/index.html.erb
The app was created Rails 3 and ruby 1.9.3p429.
Edit:
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
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")
}
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 %>
rake routes:
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
I assumed, when I click on the link from the app/views/products/show.html.erb:
<%= link_to "Reviews", product_reviews_path(@product) %>
it will take me to the 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>
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"}
My app/controllers/reviews_controller.rb as follows:
class ReviewsController < ApplicationController
def index
@reviews = @products.reviews
end
end
I believe I'm doing a trivial mistake.
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
回答1:
Solved. Aash Dhariya helped to solve this in the mongoid google groups.
I did change reviews_controller.rb:
class ReviewsController < ApplicationController
def index
@product = Product.find(params[:product_id])
@reviews = @product.reviews
end
end
and 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! :)
来源:https://stackoverflow.com/questions/16952256/accessing-sub-documents-on-rails-3-using-mongoid