Simple_fields_for 与 cocoon 不渲染

发布于 2025-01-12 03:56:11 字数 1783 浏览 2 评论 0原文

我在 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

但是,没有显示任何内容,我也不知道为什么。

我尝试过的事情:

  1. 添加rides_build 我尝试将其添加到预订控制器中
  def new
    @booking = Booking.new
    @booking.build_rides
  end

,但它只给了我一个错误“#Booking:0x000000010575c578的未定义方法`build_rides'”

  1. 添加rides.build
  def new
    @booking = Booking.new
    @booking.rides.build
  end

这也给了我一个错误消息:“未定义方法`model_name' for nil:NilClass"

  1. 仅添加 ride
  def new
    @booking = Booking.new
    @booking.build
  end

这只是没有做任何事情。没有报错,但是问题没有解决。

  1. 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:

  1. 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"

  1. 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"

  1. 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.

  1. Change ApplicationRecord to ActiveRecord::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 技术交流群。

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

发布评论

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

评论(1

寻找我们的幸福 2025-01-19 03:56:11

选项 2 是正确的方法并且应该有效,但是您的 Ride 模型中有一个错误,它应该显示

 belongs_to :booking 

注意:simple_fields_for 迭代嵌套的子项,只要有没有,什么都不会被渲染。

Option 2 is the correct way and should work, but you have an error in your Ride model, it should say

 belongs_to :booking 

Note: the simple_fields_for iterates over nested children, as long as there are none, nothing will get rendered.

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