· 2 min read

Screencast: Mongoid

Download:

Download(16.7 MB, 11:08) Alternativer Download für iPod & Apple TV(15.7 MB, 11:08)

Resourcen:

Quellcode:

[bash] bundle rails g mongoid:config rails g scaffold article name:string content:text rails g model comment name:string content:text rails g controller comments rails g scaffold author name:string [/bash]

[ruby] # Gemfile gem ‘mongoid’, ‘2.0.0.beta.19’ gem ‘bson_ext’

# models/article.rb class Article include Mongoid::Document field :name field :content field :published_on, :type => Date validates_presence_of :name embeds_many :comments referenced_in :author end

# models/comment.rb class Comment include Mongoid::Document field :name field :content embedded_in :article, :inverse_of => :comments end

# models/author.rb class Author include Mongoid::Document field :name key :name references_many :articles end

# comments_controller.rb def create @article = Article.find(params[:article_id]) @comment = @article.comments.create!(params[:comment]) redirect_to @article, :notice => “Comment created!” end [/ruby]

[html]

<%= f.label :published_on %>
<%= f.date_select :published_on %>
<%= f.label :author_id %>
<%= f.collection_select :author_id, Author.all, :id, :name %>

<% if @article.comments.size > 0 %>

Comments

<% for comment in @article.comments %>

<%= comment.name %>

<%= comment.content %>

<% end %> <% end %>

New Comment

<%= form\_for \[@article, Comment.new\] do |f| %>

<%= f.label :name %> <%= f.text\_field :name %>

<%= f.text\_area :content, :rows => 10 %>

<%= f.submit %>

<% end %> \[/html\]
Back to Blog