简体   繁体   中英

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

I have two models as x and y, such that:

y.rb:

class y < ActiveRecord::Base  
  belongs_to :x  
end

x.rb:

class X < ActiveRecord::Base  
  has_many :Ys
end

my controller will be:
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:

undefined method `attributes=' for ["s", "1233"]:Array

params[:y] looks to be an array of arrays; that is, for each instance of Y described in params, there's a separate array. Therefore when you do @y.each , you're iterating over a bunch of arrays, not a bunch of Ys.

It seems that t is an array not an ActiveRecord object.

From your pseudo-code bug is here

@y = (params[:y])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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