Rails + MongoMapper + EmbeddedDocument窗体帮助 [英] Rails + MongoMapper + EmbeddedDocument form help

查看:170
本文介绍了Rails + MongoMapper + EmbeddedDocument窗体帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个非常简单的Web应用程序(着名的遗言),并且正在使用Rails 2.3.5 + MongoMapper 0.7.2并使用嵌入式文档。我有两个问题要问:

首先,有没有使用Rails + MongoMapper + EmbeddedDocument的示例应用程序?最好在GitHub或其他类似的网站上,以便我可以查看源代码并查看我应该在哪里领导?如果没有...



...处理此任务的最佳方法是什么?我将如何创建一个表单来处理嵌入式文档。



我正在尝试将地址添加到用户。如果您愿意的话,我可以抛弃这两种模式。



感谢您的帮助! 解决方案

以下是我在其中一个应用中使用的基本方法。问题有很多答案 - 问题是文件,答案是嵌入文件。您可以使用添加答案链接生成另一个答案字段,并使用删除链接删除一个。



_form.html.erb:

 <%form_for @problem do | f | %GT; 
<%= f.error_messages%>

< p>
<%= f.label:content%>< br />
<%= f.text_area:content,:size => '50x7'%>
< / p>

...等等...

<%= add_answer_link(add answer)%>
< div id =answers>
<%= render:partial => 'answer',:collection => @ problem.answers%>
< / div>

< p><%= f.submit提交%>< / p>
<%end%>

_answer.html.erb:

 < div class =answer> 
<%fields_for'problem [answers]',answer,:index => nil do | f | - %GT;
<%= f.label:content,答案#{answer.id}:%>
<%= f.text_field:content,:size => 50%>
<%= link_to_function(remove),$(this).up('。answer')。remove()%>
<%end - %>
< / div>

problems_helper.rb

  module ProblemsHelper 
def add_answer_link(name)
link_to_function name do | page |
page.insert_html:bottom,answers,:partial => 'answer',:object => Answer.new
结束
结束
结束

我剪掉一些实施的小部分,但应该工作。


I am working on a pretty simple web application (famous last words) and am working with Rails 2.3.5 + MongoMapper 0.7.2 and using embedded documents. I have two questions to ask:

First, are there any example applications out there using Rails + MongoMapper + EmbeddedDocument? Preferably on GitHub or some other similar site so that I can take a look at the source and see where I am supposed to head? If not ...

... what is the best way to approach this task? How would I go about creating a form to handle an embedded document.

What I am attempting to do is add addresses to users. I can toss up the two models in question if you would like.

Thanks for the help!

解决方案

Here's the basic approach I took in one of my apps. Problem has many answers - problem is a document, answer is an embedded document. You can use the "add answer" link to generate another answer field, and the "remove" link to delete one.

_form.html.erb:

<% form_for @problem do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :content %><br />
    <%= f.text_area :content, :size => '50x7' %>
  </p>

  ...etc...

  <%= add_answer_link "(add answer)" %>
  <div id="answers">
    <%= render :partial => 'answer', :collection => @problem.answers %>
  </div>

  <p><%= f.submit "Submit" %></p>
<% end %>

_answer.html.erb:

<div class="answer">
  <% fields_for 'problem[answers]', answer, :index => nil do |f| -%>
    <%= f.label :content, "Answer #{answer.id}:" %>
    <%= f.text_field :content, :size => 50 %>
    <%= link_to_function "(remove)", "$(this).up('.answer').remove()" %>
  <% end -%>
</div>

problems_helper.rb

module ProblemsHelper
  def add_answer_link(name)
    link_to_function name do |page|
      page.insert_html :bottom, "answers", :partial => 'answer', :object => Answer.new
    end
  end
end

I cut out a couple minor bits of the implementation, but that should work.

这篇关于Rails + MongoMapper + EmbeddedDocument窗体帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆