简单形式的嵌套形式的示例?

发布于 2024-11-18 22:22:19 字数 1473 浏览 2 评论 0原文

我仍在努力编写控制器和实际表单,以便能够使用可选模型将一个表单嵌套在另一个表单中?

我的消息有很多联系人

提交消息时,我想选择性地添加联系人。

我以此为例:

= simple_form_for Message.new, :remote => true do |f|
  #message_form
    = f.error_messages
    %p
      = f.input :account_name, :url => autocomplete_account_name_messages_path, :size => 40, :as => :autocomplete
    %p
      = f.input :topic, :required => true,
                :input_html => {:size => 30}

    #add_contact_btn
      = link_to "Add Contact"

      #contact_form
        = f.simple_fields_for :contactd do |fc|
        = fc.input :email
        = fc.input :first_name
        = fc.input :last_name

    = f.submit 'Give'
    = f.submit 'Request'

对于 Message.rb 模型,我有以下内容:

has_many :contacts
accepts_nested_attributes_for :contacts, :reject_if =>:all_blank

注意:当我在 simple_fields_for 中使用 :contacts 时,它不起作用,因此它是单数的。但 Accepts_nested_attributess_for 则相反。

在我的消息创建控制器中,我包含了 message.contacts.build

但现在我仍然生成零联系人。

这是我看到的从谷歌浏览器作为表单数据传递的内容:

message%5Baccount_name%5D:McKesson
message%5Btopic%5D:testing a contact
message%5Bbody%5D:testing this
sender_id:
receiver_id:23
message%5Bcontacts%5D%5Bemail%5D:[email protected]
message%5Bcontacts%5D%5Bfirst_name%5D:Ang
message%5Bcontacts%5D%5Blast_name%5D:Name

I am still struggling both writing the controller and the actual form to be able to nest one form in another with an optional model?

I have Message which has many contacts

When submitting a message, I want to add a contact optionally.

I have this as an example:

= simple_form_for Message.new, :remote => true do |f|
  #message_form
    = f.error_messages
    %p
      = f.input :account_name, :url => autocomplete_account_name_messages_path, :size => 40, :as => :autocomplete
    %p
      = f.input :topic, :required => true,
                :input_html => {:size => 30}

    #add_contact_btn
      = link_to "Add Contact"

      #contact_form
        = f.simple_fields_for :contactd do |fc|
        = fc.input :email
        = fc.input :first_name
        = fc.input :last_name

    = f.submit 'Give'
    = f.submit 'Request'

For Message.rb model, I have the following:

has_many :contacts
accepts_nested_attributes_for :contacts, :reject_if =>:all_blank

Note: When I used :contacts in the simple_fields_for it didn't work, so it is singular. But the reverse for accepts_nested_attributess_for.

In my create controller for message, I included message.contacts.build

But right now I am still generating nil contacts.

Here is what I see passed as form data from google chrome:

message%5Baccount_name%5D:McKesson
message%5Btopic%5D:testing a contact
message%5Bbody%5D:testing this
sender_id:
receiver_id:23
message%5Bcontacts%5D%5Bemail%5D:[email protected]
message%5Bcontacts%5D%5Bfirst_name%5D:Ang
message%5Bcontacts%5D%5Blast_name%5D:Name

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

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

发布评论

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

评论(4

辞别 2024-11-25 22:22:19

正确的方法名称是 simple_fields_for (注意复数)

此外,您需要保留 f. 在 simple_form 对象上调用它

The correct method name is simple_fields_for (notice the plural)

Also, you need to keep the f. to call it on the simple_form object

溇涏 2024-11-25 22:22:19

我有一个小项目,在其中演示如何以简单形式使用嵌套表单,并结合 cocoon(我创建的用于动态添加/删除嵌套元素的 gem)。

该项目位于 github 上。

希望这有帮助。

I have a small project where I demonstrate how to use nested forms in simple-form, combined with cocoon (a gem I created to add/remove nested elements dynamically).

The project is on github.

Hope this helps.

枯寂 2024-11-25 22:22:19

在我的消息创建控制器中,我包含了 message.contacts.build

但现在我仍然没有产生任何联系。

确保您在 Message.rb 模型中添加了接受属性的能力。

class Message < ActiveRecord::Base
    attr_accessible :contacts_attributes
    has_many :contacts
    accepts_nested_attributes_for :contacts

我知道它并不能完全回答你的问题,但可能就是这样。当涉及到我的项目时,如果我不包含 :contacts_attributes ,它将返回 nil,在我的例子中它处理产品。即使我现在不使用简单的形式,希望这会有所帮助!

In my create controller for message, I included message.contacts.build

But right now I am still generating nil contacts.

Make sure you put in your Message.rb model the ability for it to accept the attributes too.

class Message < ActiveRecord::Base
    attr_accessible :contacts_attributes
    has_many :contacts
    accepts_nested_attributes_for :contacts

I know it doesn't answer your question fully but it may have just been this. When it comes to my project, it would return nil if i didn't include the :contacts_attributes, in my case it deals with products. Hope this helps even if I'm not using simple form as of now!

七色彩虹 2024-11-25 22:22:19

我在使用嵌套表单时遇到了类似的问题。正如 JustinRoR 所建议的,你需要定义
attr_accessible:联系人属性。

您应该能够在 ruby​​ 控制台中测试哈希(我不确定您是否尝试过此操作)。我建议您打印 params[:message] 并使用它从控制台创建消息,如 Message.new(params[:message])。 (注意 params[:message] 是通过打印 params[:message] 哈希值得到的)。

一旦它在控制台中工作,它应该像魅力一样工作

I faced similar issues working with nested forms. As suggested by JustinRoR you need to define
attr_accessible: contacts_attributes.

You should be able to test the hash in the ruby console ( I am not sure if you have tried this). I suggest you to print the params[:message] and use this to create message from console like Message.new(params[:message]). (Note params[:message] is what you get by printing the params[:message] hash).

Once it works in console it should work like charm

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