Rails 3.2 两种形式,一种隐藏

发布于 2024-12-28 08:09:19 字数 1199 浏览 1 评论 0原文

我的表单中有两个部分,并且有一个可以切换其可见性的按钮。有没有一种方法可以限制提交按钮从隐藏按钮发送参数?不幸的是,它创建的课程没有名称或编号,并且如果我使用 collection_select,它不会选择现有课程。

projects/new.html.haml

= form_for [@user, @project] do |f|

  # This part of the form is mostly shown to the user, but is failing to work correctly
  = f.collection_select :course_id, @courses, :id, :name, { prompt: true }

  # This part of the form is typically hidden, javascript reveals it.
  .hidden
    = f.fields_for :course do |builder|
      = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
      = builder.label :number, 'Number'
      = builder.text_field :number, class: 'new_project_course_number', placeholder: 'Ex: MATH-101'
      = builder.hidden_field :user_id, value: current_user.id

project.rb

belongs_to :user
belongs_to :course

attr_accessible :course_id, :course_attributes
accepts_nested_attributes_for :course

course.rb

belongs_to :user
has_many :projects

user.rb

has_many :projects
has_many :courses

如果我我不小心遗漏了任何重要信息。

I have two sections in a form, and I have a button that toggles their visibility. Is there a way to restrict the submit button from sending parameters from the hidden one? It's unfortunately creating courses without names or numbers, and it is not selecting an existing course if I use the collection_select.

projects/new.html.haml

= form_for [@user, @project] do |f|

  # This part of the form is mostly shown to the user, but is failing to work correctly
  = f.collection_select :course_id, @courses, :id, :name, { prompt: true }

  # This part of the form is typically hidden, javascript reveals it.
  .hidden
    = f.fields_for :course do |builder|
      = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
      = builder.label :number, 'Number'
      = builder.text_field :number, class: 'new_project_course_number', placeholder: 'Ex: MATH-101'
      = builder.hidden_field :user_id, value: current_user.id

project.rb

belongs_to :user
belongs_to :course

attr_accessible :course_id, :course_attributes
accepts_nested_attributes_for :course

course.rb

belongs_to :user
has_many :projects

user.rb

has_many :projects
has_many :courses

Please let me know if I am leaving off any vital information accidentally.

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

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

发布评论

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

评论(1

羞稚 2025-01-04 08:09:19

我认为您可能正在为嵌套属性集寻找 reject_if 参数。
例如:

accepts_nested_attributes_for : course, :reject_if => proc { |attributes| 
  attributes['name'].blank? 
}

或者类似的东西。它允许您按原样保留提交的表单,但仅在预设名称时创建嵌套课程对象(在您的情况下,有一些占位符,因此您可以在此处使用另一个检查)

I think you might be looking for reject_if param for your nested attribute set.
For example:

accepts_nested_attributes_for : course, :reject_if => proc { |attributes| 
  attributes['name'].blank? 
}

Or something like this. It allows you leave submitted form as is it, but only created nested course object, when name is preset (in your case, there is some placeholder, so you might use another check here)

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