Rails 3.2 两种形式,一种隐藏
我的表单中有两个部分,并且有一个可以切换其可见性的按钮。有没有一种方法可以限制提交按钮从隐藏按钮发送参数?不幸的是,它创建的课程没有名称或编号,并且如果我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能正在为嵌套属性集寻找
reject_if
参数。例如:
或者类似的东西。它允许您按原样保留提交的表单,但仅在预设名称时创建嵌套课程对象(在您的情况下,有一些占位符,因此您可以在此处使用另一个检查)
I think you might be looking for
reject_if
param for your nested attribute set.For example:
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)