加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 未定义的方法`tripphoto_changed?’为… Carr

发布时间:2020-12-17 03:54:34 所属栏目:百科 来源:网络整理
导读:我正在尝试通过关联添加新的旅行时保存图像. tripphoto.rb有一个belongs_to:trip 和我的trip.rb有一个has_many:tripphotos.此外,Carrierwave文档需要更多的东西,如上传器和我已正确设置的(它没有关联工作!). 除此之外,我有trip_uploader:tripphoto,tripp
我正在尝试通过关联添加新的旅行时保存图像.

tripphoto.rb有一个belongs_to:trip

和我的trip.rb有一个has_many:tripphotos.此外,Carrierwave文档需要更多的东西,如上传器和我已正确设置的(它没有关联工作!).

除此之外,我有trip_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?’对于#`.一个快速的谷歌没有合适的结果.有什么建议?

P.S这是我的Tripphoto表:

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

提前致谢,

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

Trip.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 %>

解决方法

这里有两个问题:

>你应该在将存储文件名的字段上安装上传器,在这种情况下,在Tripphoto模型中的文件名字段,而不是tripphoto关联.>你忘记了.在fields_for前面,看看你为标签做了什么

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读