ruby-on-rails - 使用 mongoid/mongodb/rails 访问引用字段

标签 ruby-on-rails ruby ruby-on-rails-3 mongodb mongoid

如果我有一个模型...

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

class Topic
  include Mongoid::Document
  field :category

  referenced_in :post
end

我需要在 index.html.erb 中编写什么代码才能访问主题中的数据或添加要发布的主题。

我尝试了 post.topic,但出现未定义方法错误。

非常感谢。

编辑:

这是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>

这是 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

编辑:

我还添加了相关的 _form.html.erb 代码。

<div class="field">
    <%= f.label :topic_id %>
    <%= f.collection_select :topic, Post.topic, :id, :category, :prompt => "Select a Topic" %>
</div>

编辑:

更新到2.0.0.rc.7还是无法获取。

尝试了 railscast 视频 (http://railscasts.com/episodes/238-mongoid) 中的关键方法只是为了好玩。我收到“PostsController#update 中的 BSON::InvalidObjectId”错误。

最佳答案

我猜一个主题有很多帖子?如果您想要引用关联,则需要将其更改为此。

class Post
  #...
  referenced_in :topic
end

class Topic
  #...
  references_many :posts
end

然后尝试将您的 collection_select 行更改为:

<%= f.collection_select :topic_id, Topic.all, :id, :category, :prompt => "Select a Topic" %>

关于ruby-on-rails - 使用 mongoid/mongodb/rails 访问引用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4836589/

相关文章:

ruby-on-rails - 如何更有效地聚合这些数据?

mysql - 在 Ruby 中查询 has_and_belongs_to_many 关联

ruby - Mechanize 无法正确加载页面

jquery - Rails 后台进程的进度条

ruby-on-rails - 如何返回人类可读的时间范围?

ruby-on-rails - 是否可以添加 "somewhere"一个 `before(:each)` Hook ,以便所有规范文件都可以运行它?

sql - 以文本形式查询日期时间并包含不同的时区

mysql - Rails where 查询 has_and_belongs_to_many

ruby-on-rails - 如何创建一个规范来验证 Pundit 拒绝未登录的用户?

ruby-on-rails - 如何将 Rails 应用分解为不同的小应用生态系统