开发者

adding a processing custom form field in rails 3.0 with mongomapper

I've got a Rails 3.0 project, using mongo with MongoMapper. I've got a model with basic info describing a petstore which has_many pets. A pet is a separate model.

I have a form that lets me create new petstores, but how do I add a field to create a pet at the same time as I create the new store? Right now I have a hacked in solution that accomplishes what I want, but I there's probably a Rails Way to do this, huh? How can I do it properly, so that I can use validations on the form fields and such?

My current solution involves hacking in a form field for the pet manually (added an tag with name="petstore[pet]" in the form template. This form is handled by petstore_controller's create method, and I added code to create a pet from the form field

Models:

class Petstore
  include MongoMapper::Document

  many :pets, :dependent => :destroy

  key :name, String
  key :address, String
end

class Pet
  include MongoMapper::Document

  belongs_to :petstore

  key :petstore_id, ObjectID, :required=>true
  key :type, String, :required=>true
  key :name, String 
end

_form.html.erb

<%=form_for @petstore do |f| %>
  <li>
    <%= f.label :name %>
    <%= f.text_field :name, :placeholde开发者_JAVA百科r =>"The name" %>
  </li>
  <li>
    <%= f.label :address %>
    <%= f.text_field :address, :placeholder =>'The address' %>
  </li>
  <li>
    <label for="petstore_pet">Type of pet</label>
    <input type="text" id="petstore_pet" name="petstore[pet]"> 
  <li>
    <%= f.submit "Submit" %>
  </li> 
<% end %>

petstores_controller.rb

def create
  pet = @petstore.pets.build :type => params[:petstore][:pet]
  pet.save if pet

  respond_to do |format| 
    ...
  end
end

Similar topics/questions:

  • Rails + MongoMapper + EmbeddedDocument form help

(I'm not exactly sure how to map that solution onto my question.)

  • Does MongoMapper (or any other Mongodb adapter) have a method like "accepts_nested_attributes_for"?

(Accepted answer references a google group thread that is a little over my head...)


Short answer, there is not a clean, simple way to do this.

Slightly longer answer:

Your controller will attempt to create/update a model by passing it the hash derived from the form. The key to successfully nesting forms is first to get your form to give you back the correct data. I think you have gotten this part down. Second, you need to get PetStore.create to do "the right thing" with that hash.

In MongoMapper the rules for dealing with the hash are very simple. It creates and instance of your model and then calls #{key}= value for every key/value pair in the hash. For example if MyModel is a MongoMapper Document class and I call MyModel.create(:foo=>"bar",:baz=>5) MongoMapper creates in instance of MyModel and calls foo="bar" and baz=5 on it. So long as those methods exist, MongoMapper does not care what they do. (Note the key class method creates these methods for each key.)

To get this all to work you need to create a pet= method on PetStore and have it build out a pet model.

This is not by a perfect answer, but it should be enough for you to get something to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜