与另一个常量属性的嵌套属性

发布于 2024-11-09 14:20:04 字数 471 浏览 3 评论 0原文

我有以下模型:ProjectTask。我使用嵌套属性如下:

<% form_for @project, :url => consultant_report_path(@project), :html => { :method => :put} do |f| %>
  <% f.fields_for :tasks do |builder| %>
    <%= render "due_dates_fields", :f => builder %>
  <% end %>
  <p><%= submit_tag %></p>
<% end %>

并且我有任务类型。

例如,如何使此代码仅适用于 type='economical' 的任务?

I have the following models: Project and Task. I use nested attributes as follows:

<% form_for @project, :url => consultant_report_path(@project), :html => { :method => :put} do |f| %>
  <% f.fields_for :tasks do |builder| %>
    <%= render "due_dates_fields", :f => builder %>
  <% end %>
  <p><%= submit_tag %></p>
<% end %>

And I have tasks type.

How can I make this code only for tasks with type='economical' for example?

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

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

发布评论

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

评论(2

椵侞 2024-11-16 14:20:04

您好,您可以通过多种方式完成此操作:

过滤对象:

<% f.fields_for :tasks do |builder| %>
  <% if builder.object.type == 'economical' %>
    <%= render "due_dates_fields", :f => builder %>
  <% end %>
<% end %>

指定实例:

<% @project.tasks.each do |task| %>
  <% if task.type == 'economical' %>
    <% f.fields_for :tasks, task do |builder| %>
      <%= render "due_dates_fields", :f => builder %>
    <% end %>
  <% end %>
<% end %>

或指定要使用的集合:

<% f.fields_for :tasks, @economical_taks do |builder| %>
  <%= render "due_dates_fields", :f => builder %>
<% end %>

这会有所帮助,请查看 Fields_for 了解更多信息。

Hi you can accomplish this in many ways:

Filtering object:

<% f.fields_for :tasks do |builder| %>
  <% if builder.object.type == 'economical' %>
    <%= render "due_dates_fields", :f => builder %>
  <% end %>
<% end %>

Specifying the instance:

<% @project.tasks.each do |task| %>
  <% if task.type == 'economical' %>
    <% f.fields_for :tasks, task do |builder| %>
      <%= render "due_dates_fields", :f => builder %>
    <% end %>
  <% end %>
<% end %>

Or specifying the collection to be used:

<% f.fields_for :tasks, @economical_taks do |builder| %>
  <%= render "due_dates_fields", :f => builder %>
<% end %>

Hoe this helps, look Fields_for for more information.

软的没边 2024-11-16 14:20:04

您可以将其设为自定义关系

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks

  has_many :economical_tasks, :class_name => 'Task',
                              :foreign_key => 'project_id',
                              :conditions => ['tasks.type = ?', 'economical']
  accepts_nested_attributes_for :economical_tasks
end

然后您的表单构建器可以如下所示:

<% f.fields_for :economical_tasks do |builder| %>
  <%= render "due_dates_fields", :f => builder %>
<% end %>

You can make that a custom relationship

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks

  has_many :economical_tasks, :class_name => 'Task',
                              :foreign_key => 'project_id',
                              :conditions => ['tasks.type = ?', 'economical']
  accepts_nested_attributes_for :economical_tasks
end

Then your form builder can look like this:

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