gpt4 book ai didi

ruby-on-rails - 在 Rails 中提交嵌套表单时出现 500 Internal Server Error

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:54 25 4
gpt4 key购买 nike

当我提交具有两个模型(Member 和 ChildrenInformation)的嵌套表单时,出现 500 Internal Server Error。

错误:

500 Internal Server Error

If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.

我检查了日志文件,这是我发现的:

TypeError (expected Hash (got Array) for param `child_info_attrib'):

这是我的rails项目的其他部分

查看(new.html.erb>

  <%= form_for(@member) do |f| %>
<div class="field">
<%= f.label :gender, "Gender" %><br />
<%= f.select :gender, options_for_select(["Male", "Female"]) %>
</div>
<div class="field">
<%= f.label :fname, "First Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :age, "Age" %><br />
<%= f.text_field :age %>
</div>
<div class="field">
<%= f.label :membership_id, "Membership ID" %>
<%= f.text_field :membership_id %>
</div>
<div class="field">
<%= f.label "Branch" %>
<%= f.collection_select(:branch_id, Branch.all, :id, :name) %>
</div>
<% for children_information in @member.children_informations %>
<%= fields_for "member[child_info_attrib][]", children_information do |children_info_form| %>
<div class="field">
<%= children_info_form.label :name, "Name" %><br />
<%= children_info_form.text_field :name %>
</div>
<div class="field">
<%= children_info_form.label :gender, "Gender" %><br />
<%= children_info_form.select :gender, options_for_select(["Male", "Female"]) %>
</div>
<div class="field">
<%= children_info_form.label :birthdate, "Birthdate" %><br />
nn <%= children_info_form.datetime_select :birthdate, :discard_hour => true,
:order => [:month, :day, :year], :start_year => 1900 %>
</div>
</div>
<% end %>
<% end %>`

模特(成员(member))

 class Member < ActiveRecord::Base
has_many :children_informations

def child_info_attrib=(attribs)
attribs.each do |attrib|
children_informations.build(attrib)
end
end
end

模型( child 信息)

class ChildrenInformation < ActiveRecord::Base
belongs_to :member
end

Controller (成员 Controller )

class MembersController < ApplicationController
def new
@member = Member.new
@member.children_informations.build

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @member }
end
end
def create
@member = Member.new(params[:member])
respond_to do |format|
if @member.save
format.html { redirect_to(@member, :notice => 'Member was successfully created.') }
format.xml { render :xml => @member, :status => :created, :location => @member }
else
format.html { render :action => "new" }
format.xml { render :xml => @member.errors, :status => :unprocessable_entity }
end
end
end

开发日志: http://pastebin.com/n9E2nyFv (members表有很多字段)

到目前为止我尝试的是省略嵌套形式,它确实有效。
此外,我通过 RailsCasts ( http://railscasts.com/episodes/73-complex-forms-part-1 ) 基于这种使用嵌套形式的方式。

如何使用嵌套表单成功提交?

提前致谢! :)

最佳答案

我认为您构建的嵌套表单不正确:

#app/views/members/new.html.erb
<% for children_information in @member.children_informations %>
<%= fields_for "member[child_info_attrib][]", children_information do |children_info_form| %>
<div class="field">
<%= children_info_form.label :name, "Name" %><br />
<%= children_info_form.text_field :name %>
</div>
<div class="field">
<%= children_info_form.label :gender, "Gender" %><br />
<%= children_info_form.select :gender, options_for_select(["Male", "Female"]) %>
</div>
<div class="field">
<%= children_info_form.label :birthdate, "Birthdate" %><br />
nn <%= children_info_form.datetime_select :birthdate, :discard_hour => true,
:order => [:month, :day, :year], :start_year => 1900 %>
</div>
</div>
<% end %>
<% end %>

你需要这样做:

查看

<%= f.fields_for :children_informations do |children_info_form| %>
<div class="field">
<%= children_info_form.label :name, "Name" %><br />
<%= children_info_form.text_field :name %>
</div>
<div class="field">
<%= children_info_form.label :gender, "Gender" %><br />
<%= children_info_form.select :gender, options_for_select(["Male", "Female"]) %>
</div>
<div class="field">
<%= children_info_form.label :birthdate, "Birthdate" %><br />
nn <%= children_info_form.datetime_select :birthdate, :discard_hour => true,
:order => [:month, :day, :year], :start_year => 1900 %>
</div>
</div>
<% end %>

这将创建具有正确数据的表单元素,但我也认为您可以改进对数据的处理。由于您正在使用 Rails 3,我将等待您的评论,看看您是否希望我为它编写一些代码!

关于ruby-on-rails - 在 Rails 中提交嵌套表单时出现 500 Internal Server Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21075253/

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