Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从表单上保存两个模型不起作用ruby radrails_Ruby On Rails_Ruby_Rad - Fatal编程技术网

Ruby on rails 从表单上保存两个模型不起作用ruby radrails

Ruby on rails 从表单上保存两个模型不起作用ruby radrails,ruby-on-rails,ruby,rad,Ruby On Rails,Ruby,Rad,我是rad rails的新手。我想在同一张表格的两张表格中同时书写 我有一个表机器,nom和role作为列,还有一个表ipvfour,machine_id和ip作为列 因此,我在模型中创建了这种关系,并且它属于许多人 但是当我尝试添加新机器时,如果失败 未知属性:ip 我真的不明白为什么,有人能帮我吗 机器控制器: def创建 @machine=machine.newparams[:machine] ipvfour = @machine.ip.create(params[:ip]) respo

我是rad rails的新手。我想在同一张表格的两张表格中同时书写

我有一个表机器,nom和role作为列,还有一个表ipvfour,machine_id和ip作为列

因此,我在模型中创建了这种关系,并且它属于许多人

但是当我尝试添加新机器时,如果失败

未知属性:ip

我真的不明白为什么,有人能帮我吗

机器控制器:

def创建 @machine=machine.newparams[:machine]

ipvfour = @machine.ip.create(params[:ip])

respond_to do |format|

  if @machine.save && ipvfour.save

    flash[:notice] = 'Machine was successfully created.'

    format.html { redirect_to(@machine) }

    format.xml  { render :xml => @machine, :status => :created, :location => @machine }
否则

format.html{render:action=>new}

format.xml{render:xml=>@machine.errors,:status=>:unprocessable_entity}

结束

结束

结束

new.html.erb机器

新机器 'form',:locals=>{:f_machine=>f_machine}%>

_form.html.erb机器

‘ipvfours/form’,:locals=>{:f_-ip=>f_-ip}%>

_form.html.erb ipvfours

添加机器的页面正确显示了所有字段,但似乎写入数据库失败,原因是。。。。我希望有人能帮助我

提前感谢。

编辑:

如果需要,可以在任何控制器中编辑任何模型。有一个魔术叫做 接受谷歌it的\u嵌套\u属性\u

您的代码应该如下所示:

在控制器中:

def new 
  # .... your code ....

  # create empty machine
  @machine  = Machine.new

  # add one empty ip 
  @machine.ipvfours.build

  # .... your code ....

end

def create 

  # fill machine and ipvfours directly 
  @machine = Machine.new(params[:machine])

  respond_to do |format|

    if @machine.save

      flash[:notice] = 'Machine was successfully created.'

      format.html { redirect_to(@machine) }

      format.xml  { render :xml => @machine, :status => :created, :location => @machine }

    else

      format.html { render :action => "new" }

      format.xml { render :xml => @machine.errors, :status => :unprocessable_entity }

    end

  end

end
在你看来:

new.html.erb

_form.html.erb机器

_form.html.erb ipvfours

在您的模型中:

机器模型

致意


simon

有一件事我不明白,我应该在哪里添加代码??我的意思是我用机器的控制器来写,也许我错了,我应该用ipvfours控制器。我怎么知道whitch控制器曾经写过什么?提前谢谢。是的,我刚看到。谢谢所以我完全按照你说的做了,但我仍然有一个问题:ActiveRecord::UnknownAttributeError in MachinesController创建未知属性:ipvfour请求:{machine=>{nom=>titi,ipvfour=>{ip=>10.1.1.12},role=>dev},commit=>Create,authenticity\u token=>seoge2dql6fpl05ftl7whr2wywya34edi/uc59sI=}有人认为奇怪的是,当我填写nom、role和ip时,请求的顺序是错误的。再次感谢您的帮助检查机器中的这一行_form.html.erb:您确定您写了:ipvfours复数!!好像你在写:我是你的单数!顺便说一下:字段发送到控制器的顺序并不重要,嵌套很重要,这意味着ipvfours必须在机器内部;如果这样做,我将无法在新机器页面中看到ip字段。我真的不明白为什么。
<% form_for(@machine) do |f_machine| %>  

  <%= render :partial => 'form', :locals => { :f_machine => f_machine } %>

  <%= f_machine.submit 'Create' %>

<% end %>

<%= link_to 'Back', machines_path %>
<%= f_machine.error_messages %>

<% f_machine.fields_for :ipvfours do |f_ip| %> 
  <%= render :partial => 'ipvfours/form', :locals => { :f_ip => f_ip } %>
<% end %>
<%= f_ip.label :ip %>
<br />
<%= f_ip.text_field :ip %>
class Machine < ActiveRecord::Base

    has_many :ipvfours, :dependent => :destroy
    accepts_nested_attributes_for :ipvfours

end