-6ren">
gpt4 book ai didi

ruby-on-rails - Rails 3.2 - accepts_nested_attributes_for 和 View - 表单不出现

转载 作者:行者123 更新时间:2023-12-04 05:54:31 25 4
gpt4 key购买 nike

我要模特,用户 老师 .

用户

class User < ActiveRecord::Base
...
has_one :teacher
accepts_nested_attributes_for :teacher
end

老师
class Teacher < ActiveRecord::Base
attr_accessible :teacher_last_name
belongs_to :user
...
end

我在 也有表格 Controller / View /管理员/new_teacher.hmtl.erb
<%= form_for @user, :url => create_teacher_url, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
<% f.fields_for :teacher do |builder| %>
<div class="control-group">
<%= builder.label :teacher_last_name, "Last name", :class => "control-label" %>
<div class="controls">
<%= builder.text_field %>
</div>
</div>
<% end %>

<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>

管理员 Controller
class AdminsController < ApplicationController
def new_teacher
@user = User.new
@teacher = @user.build_teacher
end
end

所以,我有两个问题:
  • 为什么我的表格没有出现?
  • <%= builder.text_field %> 是否正确?
  • 最佳答案

    我找到了解决方案。修复起来非常容易。在 fields_for 中,您应该使用 <%= 而不是 <%。

    在我的情况下,我的观点现在看起来像:

    <%= form_for @user, :url => create_teacher_url, :html => {:class => "form-horizontal"} do |f| %>
    <%= field_set_tag do %>
    <%= f.fields_for :teacher do |builder| %>
    <div class="control-group">
    <%= builder.label :teacher_last_name, "Last name", :class => "control-label" %>
    <div class="controls">
    <%= builder.text_field :teacher_last_name %>
    </div>
    </div>

    <% end %>
    <%= f.submit "Create", :class => "btn btn-large btn-success" %>
    <% end %>

    关于ruby-on-rails - Rails 3.2 - accepts_nested_attributes_for 和 View - 表单不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683367/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com