需要帮助修改高级 Rails 食谱书中的多个模型示例

发布于 2024-08-19 13:39:39 字数 1231 浏览 5 评论 0原文

我正在开发一个简单的 Rails 应用程序供我自己用于学习目的,并且我正在尝试以 1 种形式处理 2 个模型。我按照 Advanced Rails Recipes 第 13 章中的示例进行操作,并根据自己的目的进行了一些简单的修改。

我的 2 个模型是 Invoice 和 InvoicePhoneNumber。每个发票可以有多个 InvoicePhoneNumbers。我想要做的是确保每张发票至少有 1 个与之关联的电话号码。书中的示例在每个电话号码旁边放置了一个“删除”链接(书中的任务)。我想确保最上面的电话号码旁边没有删除链接,但我不知道如何执行此操作。生成发票中电话号码列表每一行的部分模板如下;

<div class="invoice_phone_number">
  <% new_or_existing = invoice_phone_number.new_record? ? 'new' : 'existing' %>
  <% prefix = "invoice[#{new_or_existing}_invoice_phone_number_attributes][]" %>

  <% fields_for prefix, invoice_phone_number do |invoice_form| -%>
    <%= invoice_form.select :phone_type, %w{ home work mobile fax } %>
    <%= invoice_form.text_field :phone_number %>
    <%= link_to_function "remove", "$(this).up('.invoice_phone_number').remove()" %>
  <% end -%>
</div>

现在,如果我可以检测到第一个电话号码何时生成,我可以在 link_to_function 上放置一个条件,这样它就不会被执行。这将解决我的问题一半并且令人满意,尽管这意味着如果我实际上想删除第一个电话号码并保留第二个电话号码,我将不得不进行一些手动洗牌。

执行此操作的理想方法大概是在带有 javascript 的浏览器中,但我不知道如何解决此问题。当只有一个时,我需要隐藏“删除”链接,当有多个时,我需要显示所有“删除”链接。 “添加电话号码”链接中使用的 .insert_html 方法中的功能似乎不足以满足此目的。

我并不是要求一步一步的操作方法(事实上,我不想得到一个 - 我想理解这一点),但是有人对从哪里开始解决这个问题有一些建议吗?

I'm developing a simple rails app for my own use for learning purposes and I'm trying to handle 2 models in 1 form. I've followed the example in chapter 13 of Advanced Rails Recipes and have got it working with a few simple modifications for my own purposes.

The 2 models I have are Invoice and InvoicePhoneNumber. Each Invoice can have several InvoicePhoneNumbers. What I want to do is make sure that each invoice has at least 1 phone number associated with it. The example in the book puts a 'remove' link next to each phone number (tasks in the book). I want to make sure that the top-most phone number doesn't have a remove link next to it but I cannot figure out how to do this. The partial template that produces each line of the list of phone numbers in the invoice is as follows;

<div class="invoice_phone_number">
  <% new_or_existing = invoice_phone_number.new_record? ? 'new' : 'existing' %>
  <% prefix = "invoice[#{new_or_existing}_invoice_phone_number_attributes][]" %>

  <% fields_for prefix, invoice_phone_number do |invoice_form| -%>
    <%= invoice_form.select :phone_type, %w{ home work mobile fax } %>
    <%= invoice_form.text_field :phone_number %>
    <%= link_to_function "remove", "$(this).up('.invoice_phone_number').remove()" %>
  <% end -%>
</div>

Now, if I could detect when the first phone number is being generated I could place a condition on the link_to_function so it is not executed. This would half solve my problem and would be satisfactory, although it would mean that if I actually wanted to, say, delete the first phone number and keep the second, I would have to do some manual shuffling.

The ideal way to do this is presumably in the browser with javascript but I have no idea how to approach this. I would need to hide the 'remove' link when there was only one and show all 'remove' links when there is more than one. The functionality in the .insert_html method that is being used in the 'add phone number' link doesn't seem adequate for this.

I'm not asking for a step-by-step how-to for this (in fact I'd prefer not to get one - I want to understand this), but does anyone have some suggestions about where to begin with this problem?

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

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

发布评论

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

评论(2

冷情妓 2024-08-26 13:39:39

部分集合有一个计数器:

<%= render :partial => "ad", :collection => @advertisements %>  

这个
将渲染“advertiser/_ad.erb”并且
将局部变量 ad 传递给
用于显示的模板。迭代
计数器将自动生成
可用于具有名称的模板
形式为partial_name_counter。
上面例子的情况下,
模板将被输入 ad_counter

There is a counter for partial-collections:

<%= render :partial => "ad", :collection => @advertisements %>  

This
will render "advertiser/_ad.erb" and
pass the local variable ad to the
template for display. An iteration
counter will automatically be made
available to the template with a name
of the form partial_name_counter. In
the case of the example above, the
template would be fed ad_counter.

蓝天白云 2024-08-26 13:39:39

对于检测一行是否是第一行的问题,您可以在调用部分时添加一个局部变量:

<%= render :partial => 'mypartial', :locals => {:first => true}  %>

因为我猜在主文件中检测一行是否是第一行会更容易。

您还可以检测电话号码是否是唯一的,而不是检测电话号码是否是第一个。如果没有,请在所有号码旁边添加删除链接,否则,不显示删除链接。请注意,除了显示/隐藏链接之外,您还需要添加代码,以防止通过(错误)使用 URL 直接删除号码而不是使用表单来删除最后一个号码。

For your problem of detecting whether a row is the first one or not, you could add a local variable when calling the partial:

<%= render :partial => 'mypartial', :locals => {:first => true}  %>

As it would be much easier to detect in the main file, whether a row is the first or not I guess.

Instead of detecting whether a phone number is the first, you could also detect whether a phone number is the only one. If not, add remove links next to all numbers otherwise, do not display the remove link. Note that besides showing/hiding the link, you also need to add code, to prevent removing of the last number by (mis)using an URL to directly delete the number instead of using your form.

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