铁轨 + MongoMapper +嵌入式文档表单帮助

发布于 2024-08-26 17:10:43 字数 309 浏览 5 评论 0原文

我正在开发一个非常简单的 Web 应用程序(著名的最后一句话),并且正在使用 Rails 2.3.5 + MongoMapper 0.7.2 并使用嵌入式文档。我有两个问题要问:

首先,是否有使用 Rails + MongoMapper + EmbeddedDocument 的示例应用程序?最好在 GitHub 或其他类似网站上,这样我就可以查看源代码并了解我应该去哪里?如果不是……

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

我试图做的是向用户添加地址。如果你愿意的话,我可以把这两个模型扔掉。

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-09-02 17:10:43

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

_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>

issues_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

我删除了一些实现的小部分,但这应该可以工作。

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.

冷血 2024-09-02 17:10:43

现在更容易了——更新 Rails 4.1.1、ruby 2.1.1p76:

模型:

class Location
 include MongoMapper::EmbeddedDocument
  key :state, String, :default => "CA"
  key :zip, String
  timestamps!
end

class House
 include MongoMapper::Document
 timestamps!
 one :location
end

控制器:

def new
    @house = House.new
end

new.html.erb:

<%= form_for @house, url: houses_path do |house_form| %>
<p>
    <%= house_form.label :name %><br>
    <%= house_form.text_field :name %>
</p>

<%= house_form.fields_for :location do |address_fields| %>

    Street  : <%= address_fields.text_field :street %>
    Zip code: <%= address_fields.text_field :zip %>
<% end %>

<p>
    <%= house_form.submit %>
</p>

Even easier now -- update for Rails 4.1.1, ruby 2.1.1p76:

Models:

class Location
 include MongoMapper::EmbeddedDocument
  key :state, String, :default => "CA"
  key :zip, String
  timestamps!
end

class House
 include MongoMapper::Document
 timestamps!
 one :location
end

Controller:

def new
    @house = House.new
end

new.html.erb:

<%= form_for @house, url: houses_path do |house_form| %>
<p>
    <%= house_form.label :name %><br>
    <%= house_form.text_field :name %>
</p>

<%= house_form.fields_for :location do |address_fields| %>

    Street  : <%= address_fields.text_field :street %>
    Zip code: <%= address_fields.text_field :zip %>
<% end %>

<p>
    <%= house_form.submit %>
</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文