2012-11-01 16 views
6

के साथ नेस्टेड फ़ील्ड मैं mongodb का उपयोग करके नेस्टेड फ़ील्ड बनाने की कोशिश कर रहा हूं। इसके लिए मैं मणि mongomodel का उपयोग कर रहा हूं जो रूबी और मोंगोडब के साथ काम करने की अनुमति देता है और मैं गतिशील नेस्टेड फ़ील्ड बनाने के लिए मणि nested_form का उपयोग कर रहा हूं। # `ऐसाmongodb

अन्य त्रुटियों के लिए

undefined method reflect_on_association ', कि मैं वास्तव में से मेल नहीं खाता है कि मैं क्या MongoDB के साथ यहाँ क्या करना चाहते हैं इंटरनेट में पाया है: मैं निम्नलिखित समस्या हो रही है। मैं RoR के लिए नया हूं, और मुझे नहीं पता कि इसे कैसे हल किया जाए। क्या कोई मेरी मदद कर सकता है?

यहाँ मेरी मॉडल है:

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 

मेरे नियंत्रक:

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 

मेरे gemfile:

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

आधार रेल कास्ट जो मैंने उपयोग किया है वह है [रेलवेकास्ट 1 9 6] (http://railscasts.com/episodes/196-nested-model-form-part-1)। दूसरा भाग विधि reflect_on_association विधि का उपयोग करेगा, और उसी समस्या को देगा जो मुझे मणि नेस्टेड_फॉर्म के साथ सामना कर रहा है। – lucianthomaz

+0

मुझे मणि [mongoid] (http://mongoid.org/en/mongoid/index.html) का उपयोग करके जो चाहिए था वह मिला। मैंने सोचा कि यह संभव नहीं था, लेकिन मैं गलत था। मोंगोइड ने पूरी तरह से किया जो मैं करने की कोशिश कर रहा था, मोंगोमोडेल से कहीं ज्यादा आसान और बिना किसी परेशानी के। – lucianthomaz

+2

अपने समाधान के उत्तर के रूप में अपने प्रश्न के उत्तर के रूप में अपना समाधान जोड़ें। फिर आप अपना जवाब भी स्वीकार कर सकते हैं और प्रश्न को ठीक से बंद कर सकते हैं। – JohnnyHK

उत्तर

1

मैं इस railscast पालन करने के लिए कोशिश कर रहा था, लेकिन मैं MongoDB के साथ काम करना चाहता था, और मैं मणि mongomodel उपयोग कर रहा था कि क्या करना है। मैं जो चाहता था वह करने में सक्षम नहीं था क्योंकि मुझे ऊपर वर्णित त्रुटि मिली है, और यहां तक ​​कि "निकालें" बटन के लिए मुझे एक और त्रुटि मिली है जिसे मैं अब याद नहीं कर सकता। जो भी हो, मैं वास्तव में ऐसा नहीं कर सका जो मैं मोंगोमोडेल का उपयोग करना चाहता था, इसलिए मैंने mongoid का उपयोग करके ऐसा करने की कोशिश की और सबकुछ आसान था।