【问题标题】:Nested fields with mongodb使用 mongodb 嵌套字段
【发布时间】:2012-11-01 17:43:40
【问题描述】:

我正在尝试使用 mongodb 创建嵌套字段。为此,我使用 gem mongomodel,它允许使用 ruby​​ 和 mongodb,我使用 gem nested_form,创建动态嵌套字段。我遇到了以下问题:

undefined methodreflect_on_association' 代表#`

我在互联网上发现的其他类似错误与我想在此处使用 mongodb 执行的操作并不匹配。我是 RoR 的新手,我不知道如何解决这个问题。谁能帮帮我?

这是我的模型:

调查.rb

class Survey < MongoModel::Document
  property :name, String
  property :questions, Collection[Question]
  timestamps!

  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

问题.rb

class Question < MongoModel::Document
  property :content, String
  timestamps!
end

我的控制器:

surveys_controller.rb
class SurveysController < ApplicationController
  # GET /surveys
  # GET /surveys.json
  def index
    @surveys = Survey.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @surveys }
    end
  end

  # GET /surveys/1
  # GET /surveys/1.json
  def show
    @survey = Survey.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @survey }
    end
  end

  # GET /surveys/new
  # GET /surveys/new.json
  def new
    @survey = Survey.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @survey }
    end
  end

  # GET /surveys/1/edit
  def edit
    @survey = Survey.find(params[:id])
  end

  # POST /surveys
  # POST /surveys.json
  def create
    @survey = Survey.new(params[:survey])

    respond_to do |format|
      if @survey.save
        format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
        format.json { render json: @survey, status: :created, location: @survey }
      else
        format.html { render action: "new" }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /surveys/1
  # PUT /surveys/1.json
  def update
    @survey = Survey.find(params[:id])

    respond_to do |format|
      if @survey.update_attributes(params[:survey])
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /surveys/1
  # DELETE /surveys/1.json
  def destroy
    @survey = Survey.find(params[:id])
    @survey.destroy

    respond_to do |format|
      format.html { redirect_to surveys_url }
      format.json { head :no_content }
    end
  end
end

我的宝石文件:

source 'https://rubygems.org'

gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem "mongomodel"
gem "bson_ext"
gem "nested_form"

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

我对调查的看法:

_form.html.erb
<%= nested_form_for(@survey) do |f| %>
  <% if @survey.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

      <ul>
      <% @survey.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <p>
    <%= f.fields_for :questions do |builder| %>
    <p>
        <%= builder.label :content, "Question" %>
    <%= builder.text_area :content, :rows => 3 %>
    </p>    
    <% end %>
    <p><%= f.link_to_add "Add a Question", :questions %></p>
  </p>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

【问题讨论】:

  • 我使用的基础导轨铸件是railscast 196。第二部分将使用方法reflect_on_association,并且会给出我在使用gem nested_form 时遇到的同样问题。
  • 我通过使用 gem mongoid 得到了我想要的。我认为这是不可能的,但我错了。 Mongoid 完美地完成了我想做的事情,比 mongomodel 容易得多,而且没有任何麻烦。
  • 将您的解决方案添加为您问题的答案,而不是仅仅将其放在评论中。然后你也可以接受你的答案并正确地结束问题。
  • 将问题标记为已解决的正确方法如上文 JohhnyHK 所述。

标签: ruby-on-rails ruby mongodb nested-forms nested-form-for


【解决方案1】:

我试图关注这个railscast,但我想使用 mongodb,我正在使用 gem mongomodel 来做到这一点。我无法做我想做的事,因为我收到了上述错误,即使是“删除”按钮,我也遇到了另一个我现在不记得的错误。无论如何,我真的无法使用 mongomodel 做我想做的事,所以我尝试使用 mongoid 来做这件事,一切都变得更容易了。

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多