Accepts_nested_attributes_for 与 formattastic 和回形针结合使用

发布于 2024-09-29 08:57:07 字数 2584 浏览 7 评论 0原文

我正在尝试建立一个简单的一对多关联。我正在尝试通过嵌套表单更新属于相册的照片:

相册的编辑表单:

<%= semantic_form_for(@album, :url => user_album_path(@user, @album), :html => {:method => :put} ) do |f| %>
 <%= f.inputs do %>
  <%= f.input :title %>
  <%= f.input :description %>
 <% end %>
 <%= f.inputs :for => :photos do |builder|%>
  <%= builder.input :_destroy, :as => :boolean %>
 <% end %>
 <%= f.submit "Submit", :disabled_with => 'Submiting...' %>
<% end %>

它可以同时工作,也不能同时工作。显然,标题和描述没有问题,并且 formattastic 正确地为相册中的每张照片制作了复选框。但在这里我已经有了第一个问题:

1)如何在复选框旁边显示照片? 我自己设法解决了这个问题:

<%= image_tag(builder.object.image.url(:album)) %>

显示图像。

这里是相册和相册模型:

class Album < ActiveRecord::Base

  belongs_to          :user
  has_many            :photos, :dependent => :destroy

  #attr_accessible     :title, :description 

  validates_presence_of :title, :description

  accepts_nested_attributes_for :photos, :allow_destroy => true
end

以及照片模型:

class Photo < ActiveRecord::Base
  belongs_to        :user
  belongs_to        :album

  has_attached_file :image, :styles => { :original => ["441x800>", :png], 
                                         :album => ["140x140#", :png], 
                                         :tiny => ["16x16#", :png] }

  validates_attachment_presence :image
  validates_attachment_content_type :image, :content_type => ["image/jpeg", "image/png", "image/gif"]
 end

相册控制器:

def update
    @user = User.find(params[:user_id])
    @album = @user.albums.find(params[:id])
    if @album.update_attributes(params[:album][:photos_attributes])
      flash[:success] = t('users.flash.album_updated')
      redirect_to @user
    else
      render :edit
    end
end

抛出的错误:

ActiveRecord::UnknownAttributeError in AlbumsController#update

unknown attribute: 0
Rails.root: /Users/stefanohug/orangerie

Application Trace | Framework Trace | Full Trace
app/controllers/albums_controller.rb:50:in `update'
Request

Parameters:

{"_snowman"=>"☃",
 "_method"=>"put",
 "authenticity_token"=>"bE4AidmbaVoG9XBqolCxheyWtd7qeltkIpMRgd8c4Fw=",
 "album"=>{"title"=>"lol",
 "description"=>"hihi",
 "photos_attributes"=>{"0"=>{"_destroy"=>"1",
 "id"=>"72"},
 "1"=>{"_destroy"=>"1",
 "id"=>"73"},

第 50 行对应于 update_attributes 行。

感谢您的帮助。

斯特夫

I am trying I have a simple one-to-many association. I am trying to update photos that belong to an album through a nested form:

The edit form for the photo album:

<%= semantic_form_for(@album, :url => user_album_path(@user, @album), :html => {:method => :put} ) do |f| %>
 <%= f.inputs do %>
  <%= f.input :title %>
  <%= f.input :description %>
 <% end %>
 <%= f.inputs :for => :photos do |builder|%>
  <%= builder.input :_destroy, :as => :boolean %>
 <% end %>
 <%= f.submit "Submit", :disabled_with => 'Submiting...' %>
<% end %>

It works and doesnt work at the same time. Obviously there is no problem with the title and description, and formtastic correctly makes a checkbox for every photo in the album. But here I already have my first question:

1) How can I display the photo next to the checkbox?
I managed to solve that myself:

<%= image_tag(builder.object.image.url(:album)) %>

Displayes the image.

Here the album and album model:

class Album < ActiveRecord::Base

  belongs_to          :user
  has_many            :photos, :dependent => :destroy

  #attr_accessible     :title, :description 

  validates_presence_of :title, :description

  accepts_nested_attributes_for :photos, :allow_destroy => true
end

And the photo model:

class Photo < ActiveRecord::Base
  belongs_to        :user
  belongs_to        :album

  has_attached_file :image, :styles => { :original => ["441x800>", :png], 
                                         :album => ["140x140#", :png], 
                                         :tiny => ["16x16#", :png] }

  validates_attachment_presence :image
  validates_attachment_content_type :image, :content_type => ["image/jpeg", "image/png", "image/gif"]
 end

The album controller:

def update
    @user = User.find(params[:user_id])
    @album = @user.albums.find(params[:id])
    if @album.update_attributes(params[:album][:photos_attributes])
      flash[:success] = t('users.flash.album_updated')
      redirect_to @user
    else
      render :edit
    end
end

The error thrown out:

ActiveRecord::UnknownAttributeError in AlbumsController#update

unknown attribute: 0
Rails.root: /Users/stefanohug/orangerie

Application Trace | Framework Trace | Full Trace
app/controllers/albums_controller.rb:50:in `update'
Request

Parameters:

{"_snowman"=>"☃",
 "_method"=>"put",
 "authenticity_token"=>"bE4AidmbaVoG9XBqolCxheyWtd7qeltkIpMRgd8c4Fw=",
 "album"=>{"title"=>"lol",
 "description"=>"hihi",
 "photos_attributes"=>{"0"=>{"_destroy"=>"1",
 "id"=>"72"},
 "1"=>{"_destroy"=>"1",
 "id"=>"73"},

Line 50 corresponds to the update_attributes line.

Thanks for your help people.

Stef

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-10-06 08:57:07

我已经找到了错误。它在控制器中:

@album.update_attributes(params[:album][:photos_attributes])

应该读为:

@album.update_attributes(params[:album])

呃...:D

I have found the error. It was in the controller:

@album.update_attributes(params[:album][:photos_attributes])

should read:

@album.update_attributes(params[:album])

Duh... :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文