简体   繁体   English

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

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

I have two models as x and y, such that: 我有x和y两个模型,例如:

y.rb: y.rb:

class y < ActiveRecord::Base  
  belongs_to :x  
end

x.rb: x.rb:

class X < ActiveRecord::Base  
  has_many :Ys
end

my controller will be: 我的控制器将是:
x.controller.rb: 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

It's not updating the y data and giving error as: 它没有更新y数据并给出如下错误:

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

params[:y] looks to be an array of arrays; params[:y]看起来是一个数组数组; that is, for each instance of Y described in params, there's a separate array. 也就是说,对于在参数中描述的每个Y实例,都有一个单独的数组。 Therefore when you do @y.each , you're iterating over a bunch of arrays, not a bunch of Ys. 因此,当您执行@y.each ,您要遍历一堆数组,而不是一堆Ys。

It seems that t is an array not an ActiveRecord object. 看来t是一个数组,而不是ActiveRecord对象。

From your pseudo-code bug is here 来自您的伪代码错误在这里

@y = (params[:y])

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

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