Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.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 On Rails_Ruby On Rails 3_Nested Forms_Nested Attributes_Polymorphic Associations - Fatal编程技术网

Ruby on rails 您能否编辑/更新';它也是一个嵌套属性?

Ruby on rails 您能否编辑/更新';它也是一个嵌套属性?,ruby-on-rails,ruby-on-rails-3,nested-forms,nested-attributes,polymorphic-associations,Ruby On Rails,Ruby On Rails 3,Nested Forms,Nested Attributes,Polymorphic Associations,我正在创建一个相册,其中一个用户有许多相册,每个相册都有许多照片 唱片班 Class Album < ActiveRecord::Base attr_accessible :photos_attributes, :name has_many photos, :as => imageable accepts_nested_attributes_for :photos end 编辑相册表单 <%= form_for @album do |f| %> <

我正在创建一个相册,其中一个用户有许多相册,每个相册都有许多照片

唱片班

Class Album < ActiveRecord::Base

 attr_accessible :photos_attributes, :name
 has_many photos, :as => imageable
 accepts_nested_attributes_for :photos

end
编辑相册表单

<%= form_for @album do |f| %>
    <%= f.label :album_name %><br />
    <%= f.text_field :name %>

  <%= f.fields_for :photos do |photo| %>
    <%= photo.label :photo_description %>
    <%= photo.text_field :description %>

    <%= photo.label :photo_location %>
    <%= photo.text_field :location %>
  <% end %>

<% end %>
与照片属性哈希一起发送的ID是数据库中照片表中的实际ID。无论出于何种原因,如果用户编辑照片描述或位置,rails都不会更新它们。我相信这与照片的多态性有关

有人能帮忙吗??我已经试了几个小时,在网上到处搜索,但没有找到解决办法


谢谢

这可能取决于您使用的rails版本。我相信<3.2需要在has\u many关联上使用autosave属性。你可能只是想增加一些安全性,看看这是否奏效


此外,请检查日志,确保在请求过程中未收到任何警告。

您可以在问题中添加控制器中的创建/更新方法吗?只添加了相册控制器方法。不过,它们并没有什么特别之处。“创建”功能可以很好地工作,并创建带有描述和位置的照片。奇怪的是,如果我编辑相册并删除一张照片,效果会很好。唯一的问题是更新照片的模型属性(描述和位置)。我有另一个模型,它是一个嵌套属性,更新它可以很好地工作。因此,我很确定这与照片是多态的这一事实有关,并且会造成一些问题。编辑相册时,请检查rails服务器的输出。一切看起来都很好。Rails只是没有获取模型的修改描述或位置更改。这些参数看起来都不错,但最终并没有被数据库捕获并保存:(我知道我可以在控制器更新中进行黑客攻击,并通过params散列循环,因为数据在那里并手动更新……看起来Rails能够处理这个问题,但你应该在github上打开一个问题。
def create
  @album = current_user.albums.build(params[:album])
  if @album.save
    redirect_to @album, :notice => "Successfully created album."
  else
    render :action => 'new'
  end
end


def edit
  @album = Album.find(params[:id])
end


def update
  @album = Album.find(params[:id])
  if @album.update_attributes(params[:album])
    redirect_to @album, :notice  => "Successfully updated album."
  else
    render :action => 'edit'
  end
end
<%= form_for @album do |f| %>
    <%= f.label :album_name %><br />
    <%= f.text_field :name %>

  <%= f.fields_for :photos do |photo| %>
    <%= photo.label :photo_description %>
    <%= photo.text_field :description %>

    <%= photo.label :photo_location %>
    <%= photo.text_field :location %>
  <% end %>

<% end %>
  Parameters: { "album"=>{"user_id"=>"1", "name"=>"Lake Tahoe", 
                "photos_attributes"=>{"1"=>{"description"=>"Top of the Mountain!", "id"=>"2"}, 
                                      "2"=>{"description"=>"From the cabin", "id"=>"5"}}}, 
                "commit"=>"Update Ablum", "id"=>"10"}