2012-11-01 27 views
6

Yuvanlı mongodb kullanarak oluşturmaya çalışıyorum. Bunun için yakut ve mongodb ile çalışmayı sağlayan mongomodel gemini kullanıyorum ve iç içe geçmiş alanları oluşturmak için gemiyi nested_form kullanıyorum. Gerçekten ben MongoDB ile burada yapmak istediğim şey eşleşmiyor internette bulduk,İç içe geçmiş alanlar mongodb ile

Diğer hatalar böyle # `için

undefined method reflect_on_association': Aşağıdaki sorun yaşıyorum. RoR için yeniyim ve bunu nasıl çözeceğimi bilmiyorum. Biri bana yardım edebilir mi? İşte

benim modeller ise:

survey.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 

questions.rb

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

Benim denetleyicisi:

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 

Benim gemfile:

Araştırmanın
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' 

Bence: Ben bu railscast takip etmeye çalışıyordum ama ben MongoDB ile çalışmak istedim ve bunu yapmak için mücevher mongomodel kullanıyordum

_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 %> 
+0

Kullandığım temel raylar [railscast 196] (http://railscasts.com/episodes/196-nested-model-form-part-1). İkinci kısım, reflect_on_association yöntemini kullanacak ve nested_form gemiyle karşılaştığım aynı sorunu verecektir. – lucianthomaz

+0

Gem [mongoid] kullanarak istediğimi aldım (http://mongoid.org/en/mongoid/index.html). Bunun mümkün olmadığını düşündüm, ama yanılmışım. Mongoid ne yapmaya çalıştığımı, mongomodelden çok daha kolay ve sorunsuz bir şekilde yaptı. – lucianthomaz

+2

Sadece yorumda bulunmak yerine, çözümünüzü sorunuzun cevabı olarak ekleyin. Sonra cevabınızı da kabul edebilir ve soruyu düzgün bir şekilde kapatabilirsiniz. – JohnnyHK

cevap

1

. İstediğimi yapamadım çünkü yukarıda açıklanan hatayı aldım ve "Kaldır" düğmesi için bile şimdi hatırlayamadığım başka bir hata aldım. Ne olursa olsun, ben mongomodel kullanarak istediğimi yapamadım, bu yüzden mongoid kullanarak yapmaya çalıştım ve her şey daha kolaydı.