4

関連付けを介して新しいトリップを追加するときに、画像を保存しようとしています。

tripphoto.rbにはbelongs_to :trip

私の trip.rb にはhas_many :tripphotos. さらに、Carrierwave のドキュメントには、私が正しくセットアップしたアップローダなどのいくつかのものが必要です (関連付けなしで機能しました!)。

それに加えてmount_uploader :tripphoto, TripphotoUploader、私は trip.rbaccepts_nested_attributes_for :tripphotos, allow_destroy: trueに入れており、フォームを適切に機能させるために:

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

それでも、undefined method「tripphoto_changed?」というエラーが表示されます。#`用。クイック Google には適切な結果がありません。何かアドバイス?

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

Tripphoto.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

3

ここに 2 つの問題があります。

  • ファイル名を格納するフィールドにアップローダーをマウントする必要があります。その場合は、関連付けではなくモデルのfilenameフィールドです。Tripphototripphoto
  • 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 に答える