Si j'ai un modèle ...Accès à un champ référencé en utilisant MongoId/MongoDB/rails
class Post
include Mongoid::Document
field :link
field :title
field :synopsis
field :added_on, :type => Date
validates_presence_of :link
embeds_many :replies
references_one :topic
end
et
class Topic
include Mongoid::Document
field :category
referenced_in :post
end
Qu'aurais-je besoin de coder en index.html.erb d'accès données dans le sujet ou pour ajouter un sujet à poster.
J'ai essayé post.topic mais j'obtiens une erreur de méthode non définie.
Merci beaucoup.
Edit:
Voici le code index.html
<div id="post">
<% @posts.each do |post| %>
<div class="title_container">
<%= link_to post.title, post.link %> || <%= link_to 'Edit', edit_post_path(post) %> || <%= post.topic %>
</div>
<% end %>
<br />
<h2>Topics<h2>
<% for topic in @post.topics %>
<h3><%= topic.category %></h3>
<% end %>
</div>
ici est le posts_controller
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
Edit:
Je suis également la _form.html pertinente ajoutais Code .erb.
<div class="field">
<%= f.label :topic_id %>
<%= f.collection_select :topic, Post.topic, :id, :category, :prompt => "Select a Topic" %>
</div>
Edit:
Mise à jour à 2.0.0.rc.7 ne peut toujours pas obtenir.
J'ai essayé la méthode clé dans la vidéo railscast (http://railscasts.com/episodes/238-mongoid) juste pour le plaisir. Je reçois une erreur "BSON :: InvalidObjectId dans PostsController # update".
@ user593120, nous pouvons mieux vous aider si vous postez la partie pertinente de index.html.erb et méthode contoller. – hade
@ user593120 Pourriez-vous également publier le code du contrôleur? – Dogbert
@hade @Dogbert J'ai mis à jour le message original. Est-ce que cette information utile? – moctopus