繁体   English   中英

如何在Rails的ruby中的一个控制器中执行多个更新

[英]how to do more than one updates in one controller in ruby on rails

我有x和y两个模型,例如:

y.rb:

class y < ActiveRecord::Base  
  belongs_to :x  
end

x.rb:

class X < ActiveRecord::Base  
  has_many :Ys
end

我的控制器将是:
x.controller.rb:

def update
  @x = X.find(params[:id])

  @x.update_attributes(params[:x]) 
  @y = (params[:y])
  @y.each { |t| t.attributes = params[:y][t.id.to_s] }

  @x.ys.build(attributes)    
  flash[:notice] = 'X was successfully updated.'
  redirect_to :action => 'edit'          
end

它没有更新y数据并给出如下错误:

[“ s”,“ 1233”]的未定义方法`attributes =':数组

params[:y]看起来是一个数组数组; 也就是说,对于在参数中描述的每个Y实例,都有一个单独的数组。 因此,当您执行@y.each ,您要遍历一堆数组,而不是一堆Ys。

看来t是一个数组,而不是ActiveRecord对象。

来自您的伪代码错误在这里

@y = (params[:y])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM