【问题标题】:Accessing a referenced field using mongoid/mongodb/rails使用 mongoid/mongodb/rails 访问引用的字段
【发布时间】:2011-01-29 11:26:42
【问题描述】:

如果我有模特……

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”错误。

【问题讨论】:

  • @user593120,如果你把index.html.erb和contoller方法的相关部分贴出来,我们可以更好地帮助你。
  • @user593120 你能把控制器代码也贴一下吗?
  • @hade @Dogbert 我已经更新了原帖。这是有用的信息吗?

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


【解决方案1】:

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

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" %>

【讨论】:

  • 我收到此错误,#<0x5e1a6f0>
【解决方案2】:

您的post.rb 文件有一个references_one :topic,但在您的索引视图中,您正在执行for topic in @post.topics,这意味着一个帖子可以有很多主题。要么您需要将模型更改为 references_many :topics,要么将视图更改为每个帖子只有一个主题。

【讨论】:

  • 我将索引更改为仅使用 并且没有返回任何值,这让我相信我的代码可能没有保存所选类别?
【解决方案3】:

@user593120 你有没有在你的 index.html.erb 中尝试过这个

<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>
    <% @posts.each do |post| %>
        <h3><%= post.topic.category  if post.topic %></h3>
    <% end %>


</div>

【讨论】:

  • 当我这样做时,我什么也得不到。这让我觉得当我创建/更新帖子时,topic.category 选择没有保存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 2011-07-22
相关资源
最近更新 更多