Mongoid creating embedded document from a view

I am trying to add a embed a Profile into a User and I keep getting this error.

Access to the collection for Profile is not allowed since it is an embedded document, please access a collection from the root document.

I am sure it's a simple problem to fix but I have no clue how to do it. I am very new with RoR so things are still a little confusing. Here is my code.

Models/Profile

class Profile include Mongoid::Document attr_accessible :handle, :description field :handle field :description embedded_in :user end

Controllers/Profile

class ProfileController < ApplicationController def create @user = current_user @profile = @user.profile.create!(params[:profile]) redirect_to dashboard_path end end

Views/profile/new

<h1>Create Profile</h1> <%= form_for [:current_user, Profile.create] do |f| %> <div class="field"> <%= f.label :handle %> <%= f.text_field :handle %> </div> <div class="field"> <%= f.label :description %> <%= f.text_area:description %> </div> <p class="button"><%= f.submit %></p> <% end %>

-------------Problems Reply------------

You cannot use Profile.create in your views.html.erb because Profile is embedded in a user. So you need to do something like current_user.build_profile

<%= form_for [:current_user, current_user.build_profile] do |f| %>

should work

try

@user = current_user
@profile = Profile.new(params[:profile])
@user.profile = @profile
@user.save
# or @profile.save

Category:ruby on rails Views:0 Time:2011-05-21

Related post

  • Factory Girl, Mongoid, and Embedded Documents 2011-05-13

    I am setting up my first app with Mongoid and Devise. I am trying to Factory a user in my test with this factory: Factory.define(:user) do |f| f.email "[email protected]" f.password "testing" f.password_confirmation "testing" end When I try and fac

  • Mongoid: convert embedded document into referenced/own collection 2012-03-16

    I need to convert an embedded document onto its own collection, so it can be referenced from another collection. Lets suppose I have a Parent that embeds many Childs. I was thinking of something along this: Parent.all.each do |p| p.childs.all.each do

  • Mongoid deleting embedded documents does not persist 2012-04-02

    I have been running into an issue for hours and have Googled myself senseless. I have a Mongoid model with an embedded document, like so: embeds_many :tags, :as => :taggable For some reason, attempting to delete this document appears to work in th

  • Mongoid same embedded documents types for different fields 2012-04-17

    I am attempting to add embedded documents. I know this syntax is wrong but it demonstrates what I am trying to accomplish. class Email include Mongoid::Document embeds_many :recipients, as: :to embeds_many :recipients, as: :cc embeds_many :recipients

  • Mongoid find embedded document 2013-03-10

    I'm trying to search for an embedded document by its id, and return it. This is possible, but only, as far as I see, by using mongo to find the document which embeds it, and then searching that document in ruby for the embedded document I'm after. Li

  • mongoid save embedded documents 2011-11-11

    I'm trying to build up on the following tutorial from railscast: http://railscasts.com/episodes/196-nested-model-form-part-1 I'm trying to make everything work with mongodb and mongoid. the scenario is: I want to creates events linked to a location.

  • How to creating an in-place editable form for embedded documents using mongoid in Rails3? 2011-07-26

    I have two models User(main document) and Contact(embedded document in User). I'm showing a list of contacts when displaying a user. I was wondering if there is a suggested way of creating an in place editable form for all the contacts that are shown

  • Creating Mongoid embedded documents 2012-04-16

    I am using Mongoid for the first time. I want to store a collection of emails that have a subject, body, and arrays of to, cc, and bcc recipients. Example: {to: [{email: '[email protected]', name: 'Andrew'}], cc: ... However, I can't seem to figure

  • Mongoid - getting all attributes including embedded documents 2010-07-22

    Is there an easy way to get all attributes of a Mongoid document, including those of embedded documents? For example, if I have the following documents: class Person include Mongoid::Document embeds_many :phone_numbers field :name end class PhoneNumn

  • Dirty Tracking of embedded document on the parent doc in Mongoid 2010-11-23

    I had to track the dirty objects. And it works fine with the parent doc. But when I change the embedded or referenced in doc, the dirty has to be accessed via the embedded/referenced in doc itself. How can I track the dirty on the parent doc itself??

  • How to enforce unique embedded document in mongoid 2010-12-02

    I have the following model class Person include Mongoid::Document embeds_many :tasks end class Task include Mongoid::Document embedded_in :commit, :inverse_of => :tasks field :name end How can I ensure the following? person.tasks.create :name =

  • Mongoid: Run callback from embedded document on parent 2010-12-08

    Rails 3.0.1 Mongoid (2.0.0.beta.20) Class Post embeds_many :comments field :comments_count end Class Comment embedded_in :commentable, :inverse_of => :comments end I want to select the 10 most commented posts. To do that I need comments_count fiel

  • Rails and mongoid: What is going on with RC7 and embedded documents? 2011-02-20

    Until now I was using rc6 and I decided to upgrade, but it's totally breaking my app ? Maybe I am doing something wrong, but I believe I followed the documentation. I have a model Content that embeds_many Localized_Content. Once I have a content crea

  • Mongoid, confused with embedded document with timestamps and versioning? 2011-04-11

    I have been using Mongoid for about 3 months now, and I have managed to get done pretty much anything I need thanks to the great document and resources out there. But going back to improve some stuff I have made a few backs, I am definitely strugglin

  • Writing new embedded document in Mongoid fails mysteriously 2011-10-03

    I'm trying to embed a new document in a previously existing document. When the object was created, the root document looked like this: class MongoPoll include Mongoid::Document embeds_one :region_count, :as => :voteable, :class_name => 'VoteCou

  • Mongoid _destroy is not deleted embedded document using nested_form gem 2011-11-02

    I am on Rails 3.1, Mongoid 2.3.3, and using the nested_form gem. In my form, I have the nested_form link_to_add and link_to_remove set up to add and remove an embedded document in my model. The link_to_add helper method works great, but the link_to_r

  • Mongoid: persistence in 1 to many relation within embedded document 2011-11-09

    I have a problem with relationships persisting in an embedded document. Here is the example require 'mongoid' require 'mongo' class User include Mongoid::Document field :name key :name embeds_one :garage end class Garage include Mongoid::Document fie

  • Mongoid: embedded documents automatically initializing on construction of parent 2011-11-09

    Is there a way to get embedded documents to initialize automatically on construction in mongoid? What I mean is given that User which embeds a garage document. I have to write the following code to fully set up the user with the garage: user = User.c

  • Rails 3 and Mongoid: Embedded documents validation 2012-02-20

    So, I am having some issues with user authentication in embedded documents. I have two documents, one embedded in the other. A business has many members. The models look like this: class Member include Mongoid::Document field :username, type: String

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.140 (s). 11 q(s)