Simple_fields_for 与 cocoon 不渲染
我在 Ruby on Rails 中有一个一对多关联:
class Booking < ActiveRecord::Base
has_many :rides
accepts_nested_attributes_for :rides, allow_destroy: true
end
### and ###
class Ride < ActiveRecord::Base
belongs_to :bookings
end
预订和乘车视图以及控制器是搭建的。
我想要在预订表单中添加“添加行程”按钮:
= simple_form_for(@booking) do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
.form-inputs
.container
.row
.col-6
= f.input :first_name
= f.input :phone_number
.col-6
= f.input :last_name
= f.input :email
.col-12
= f.input :company
### here is the simple fields that does not work:
= f.simple_fields_for :rides do |ride|
= render 'rides/form', :f => ride
= link_to_add_association 'add ride', f, :rides
但是,没有显示任何内容,我也不知道为什么。
我尝试过的事情:
- 添加
rides_build
我尝试将其添加到预订控制器中
def new
@booking = Booking.new
@booking.build_rides
end
,但它只给了我一个错误“#Booking:0x000000010575c578的未定义方法`build_rides'”
- 添加
rides.build
def new
@booking = Booking.new
@booking.rides.build
end
这也给了我一个错误消息:“未定义方法`model_name' for nil:NilClass"
- 仅添加
ride
def new
@booking = Booking.new
@booking.build
end
这只是没有做任何事情。没有报错,但是问题没有解决。
- 将
ApplicationRecord
更改为ActiveRecord::Base
这解决了别人的问题,但没有解决我的问题。
可能还想补充一点,simple_fields_for 内绝对没有任何内容被渲染,甚至连简单的 h1
也没有。
I have a one-to-many association in Ruby on Rails:
class Booking < ActiveRecord::Base
has_many :rides
accepts_nested_attributes_for :rides, allow_destroy: true
end
### and ###
class Ride < ActiveRecord::Base
belongs_to :bookings
end
The booking and ride views and controllers were scaffolded.
I wanted an "Add rides" button in my bookings form:
= simple_form_for(@booking) do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
.form-inputs
.container
.row
.col-6
= f.input :first_name
= f.input :phone_number
.col-6
= f.input :last_name
= f.input :email
.col-12
= f.input :company
### here is the simple fields that does not work:
= f.simple_fields_for :rides do |ride|
= render 'rides/form', :f => ride
= link_to_add_association 'add ride', f, :rides
However, nothing gets displayed and I don't know why.
Things I've tried:
- Add
rides_build
I tried to add this to the booking controller
def new
@booking = Booking.new
@booking.build_rides
end
But it only gave me an error "undefined method `build_rides' for #Booking:0x000000010575c578"
- Add
rides.build
def new
@booking = Booking.new
@booking.rides.build
end
This also gave me an error message: "undefined method `model_name' for nil:NilClass"
- Add only
ride
def new
@booking = Booking.new
@booking.build
end
This just didn't do anything. No error, but the problem wasn't solved.
- Change
ApplicationRecord
toActiveRecord::Base
This solved it for someone else, but not for me.
Might also want to add that absolutely nothing gets rendered that's inside the simple_fields_for, not even a simple h1
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选项 2 是正确的方法并且应该有效,但是您的
Ride
模型中有一个错误,它应该显示注意:
simple_fields_for
迭代嵌套的子项,只要有没有,什么都不会被渲染。Option 2 is the correct way and should work, but you have an error in your
Ride
model, it should sayNote: the
simple_fields_for
iterates over nested children, as long as there are none, nothing will get rendered.