4

通过关联添加新旅行时,我正在尝试保存图像。

tripphoto.rb 有一个belongs_to :trip

我的 trip.rb 有一个has_many :tripphotos. 此外,Carrierwave 文档还需要一些东西,比如上传器,我已经正确设置了这些东西(它在没有关联的情况下工作!)。

连同它,我已经进入mount_uploader :tripphoto, TripphotoUploader了我的 trip.rb 并accepts_nested_attributes_for :tripphotos, allow_destroy: true让我的表单正常工作:

    <%= fields_for :tripphotos do |photo| %>
      Img <%= photo.file_field :filename %>
    <% end %>

尽管如此,我得到错误undefined methodtripphoto_changed?对于#`。快速谷歌没有合适的结果。有什么建议吗?

PS这是我的Tripphoto表:

class Tripphoto < ActiveRecord::Base {
    :id => :integer,
    :filename => :string,
    :trip_id => :integer,
    :created_at => :datetime,
    :updated_at => :datetime
}

提前致谢,

编辑:根据要求我的代码:

行程.rb

class Trip < ActiveRecord::Base
  attr_accessible :description, :title, :user_id, 
  :triplocations_attributes, :photo, :category_ids, 
  :start_city, :tripphotos, :img_url, :thumb_url, :images,
  :tags_attributes

  # validates :title, :length => {:minimum => 3}
  # validates :description, :presence => true
  # validates_associated :tags


  has_many :triplocations, :dependent => :destroy
  has_many :tripphotos, :dependent => :destroy

  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags


  accepts_nested_attributes_for :triplocations, allow_destroy: true
  accepts_nested_attributes_for :tripphotos, allow_destroy: true
  accepts_nested_attributes_for :categories
  accepts_nested_attributes_for :tags


  mount_uploader :tripphoto, TripphotoUploader
end

旅行照片.rb

class Tripphoto < ActiveRecord::Base
  attr_accessible :filename, :trip_id
  belongs_to :trip
end

完整的旅行表格

<%= form_for @trip, :html => {:multipart => true} do |a| %> 
    <%= a.label :title, "Routetitel" %>
    <%= a.text_field :title %>

    <%= a.label :description, "Omschrijving" %>
    <%= a.text_area :description %>

    <%= a.label :start_city, "Stad" %>
    <%= a.text_field :start_city, :id => "cityName" %>

    <% for category in Category.find(:all) %>
        <div>
          <%= check_box_tag "trip[category_ids][]", category.id, @trip.categories.include?(category) %>
          <%= category.name %>
        </div>
    <% end %>
    <%= a.fields_for :tags do |f| %>
        <p>
            <%= f.label :title, 'Steekwoorden:' %>
            <%= f.text_field :title %>
        </p> 
        <div id="inputFields"></div>

    <% end %>
    <div style="background: red;">
        <%= fields_for :tripphotos do |photo| %>
          Img <%= photo.file_field :filename %>
        <% end %>
    </div>


    <%= a.submit 'Verstuur' %>
<% end %>
4

3 回答 3

6

我有同样的问题,

对我来说,是我忘了跑:

bundle exec rake db:migrate

这就是 youruploaderattribute_changed 的​​原因?穆德尔中缺少方法。

于 2013-11-26T20:44:44.013 回答
3

这里有两个问题:

  • 您应该将上传程序安装在将存储文件名的filename字段上,Tripphoto在这种情况下是模型中的字段,而不是tripphoto关联。
  • 你忘了a.前面的fields_for,看看你对标签做了什么
于 2012-11-01T15:49:48.397 回答
1

同样的问题,是由我的一个小错误引起的。首先我跑

rails generate uploader Photo

并设置

mount_uploader :photo, PhotoUploader

在模型中。但后来我在迁移中使用了“头像”而不是“照片”:

rails g migration add_avatar_to_users avatar:string

那太愚蠢了!嗯嗯嗯 ;-)

于 2014-09-13T06:22:00.050 回答