开发者

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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜