两个表单值未保存

发布于 2024-11-08 17:11:03 字数 1742 浏览 0 评论 0原文

我有这样的注册:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <%= f.fields_for :profile, resource.build_profile do |t| %>
     <div class ="field">
        <%= t.label :type, "Are you an artist or listener?" %><br />
        <div>Artist: <%= t.radio_button :type, "Profile::Artist", :id => "artist_button" %></div>
        <div>Listener: <%= t.radio_button :type, "Profile::Listener", :id => "listener_button" %></div>
      </div>
  <% end %>

  <p class="before"><%= f.submit "Sign up", :id => "new_user_submit" %></p>
<% end %>

然后我有一些动态插入字段的 JQuery:

$("#artist_button").click(function() {
        if ($('input#user_name').parent().hasClass('field')){
            $('input#user_name').parent().remove();
        }
        $(".before").before('<div class="field"><label for="user_name">Artist or Band/Group Name</label><br><input id="user_name" name="user[name]" size="30" type="text"></div>');
    }); 

但是,嵌套在用户表单中的 type 属性和 name 属性动态插入并没有保存到数据库中。输入的值为空。

这是为什么呢?我该如何解决这个问题?

更新:

名称属性不是attr_accessible,这导致它无法保存到数据库中。但是,嵌套在用户表单内的 type 属性是 attr_accessible ,但仍未保存到数据库中。

I have this signup for with devise:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <%= f.fields_for :profile, resource.build_profile do |t| %>
     <div class ="field">
        <%= t.label :type, "Are you an artist or listener?" %><br />
        <div>Artist: <%= t.radio_button :type, "Profile::Artist", :id => "artist_button" %></div>
        <div>Listener: <%= t.radio_button :type, "Profile::Listener", :id => "listener_button" %></div>
      </div>
  <% end %>

  <p class="before"><%= f.submit "Sign up", :id => "new_user_submit" %></p>
<% end %>

Then I have some JQuery that inserts a field dynamically:

$("#artist_button").click(function() {
        if ($('input#user_name').parent().hasClass('field')){
            $('input#user_name').parent().remove();
        }
        $(".before").before('<div class="field"><label for="user_name">Artist or Band/Group Name</label><br><input id="user_name" name="user[name]" size="30" type="text"></div>');
    }); 

However, the type attribute that is nested in the user form and the name attribute that is inserted dynamically are not being saved to the database. The values are being entered as null.

Why is this? How can I fix this?

UPDATE:

The name attribute was not attr_accessible and that was causing it to not be saved to the db. However, the type attribute, which is nested inside the user form, is attr_accessible but is still not being saved to the db.

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

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

发布评论

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

评论(4

滿滿的愛 2024-11-15 17:11:03

“type”是一个“神奇的”ActiveRecord 列名称,用于单表继承,这意味着您刚刚遇到了约定优于配置的问题。您需要将“type”列重命名为其他名称 - 我通常使用“kind”作为列名称。

"type" is a "magic" ActiveRecord column name used for single table inheritance which means that you just got hit by convention-over-configuration. You need to rename your "type" column to something else - I usually use "kind" as a column name.

我很坚强 2024-11-15 17:11:03

尝试放入您的用户模型:

attr_accessible :profiles_attributes

与其余设备一起 attr_accessible(email,remeber_me,etc) ,不要删除您拥有的!

Try putting in your User Model:

attr_accessible :profiles_attributes

along with the rest of devises attr_accessible(email,remeber_me,etc) , do not delete what you have!

柠北森屋 2024-11-15 17:11:03

type 是关联吗?如果是这样,您还必须将 :type_id 添加到 attr_accessible 中。

Is type an association? If so, you'd have to add :type_id to attr_accessible, too.

画离情绘悲伤 2024-11-15 17:11:03

您添加了

accepts_nested_attributes_for :profile 

用户模型吗?

Did you add

accepts_nested_attributes_for :profile 

in your User model?

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