【问题标题】:Rails carrierwave has_many errorRailscarrierwave has_many 错误
【发布时间】:2015-07-09 00:54:00
【问题描述】:

每个用户都有_one 个字符。每个Character has_one :profilepicture,属于Picturething 类,它持有一个Carrierwave mount_uploader 来上传单张图片。每个字符都有_many :standardpictures,也属于 Picturething 类。图片上传在views/users/edit中处理,在users_controller中点击update_pictures方法。

我们的想法是一次上传一张标准图片。好像可以了,Rails 控制台 > Picturething.all 显示一个新的 Picturething 已经添加到数据库中,并且在页面上正确显示。这旨在成为 character.standardpictures 之一。

奇怪的是,不知怎的,在整个过程中,角色的 :profilepicture 也被设置为上传的同一张图片。我不明白这是怎么回事。在任何时候我都没有代码说“@character.profilepicture = standardpicture”,但不知何故,它决定第一个 :standardpicture 和 :profilepicture 是一个相同的。如果 profilepicture 存在,但它不应该存在,它会显示在 edit.html.erb 页面上,我有 <% if @character.profilepicture.nil? %> 行。它在这里显示上传的图片,所以很明显 profilepicture 不是 nil,但应该是。

这是怎么回事?

字符.rb:

has_many :standardpictures, class_name: "Picturething", dependent: :destroy
accepts_nested_attributes_for :standardpictures
has_one  :profilepicture, class_name: "Picturething", dependent: :destroy
accepts_nested_attributes_for :profilepicture

picturething.rb:

class Picturething < ActiveRecord::Base
  belongs_to      :character
  mount_uploader  :picture, CharacterpicUploader
  validate        :picture_size
end

app/views/users/edit.html.erb:

<%= form_for :standardpicture, url: update_pictures_user_path, 
             method: :post, html: { multipart: true } do |f| %>
  <%= f.label :picture %>
  <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
  <%= f.submit "Upload pictures", class: "btn btn-primary" %>
<% end %>

routes.rb:

post '/users/:callsign/update_pictures', to: 'users#update_pictures',  as: :update_pictures_user

users_controller.rb:

def update_pictures
  @character = Character.find_by(callsign: params[:callsign])
  @user = @character.sociable
  @standardpicture = @character.standardpictures.build(update_pictures_user_params)
  if @standardpicture.save
    flash[:success] = "Pictures updated"
    redirect_to(edit_user_path(@user))
  else
    redirect_to(edit_user_path(@user))
  end
end # update_pictures

def update_pictures_user_params
  params.require(:standardpicture).permit(:picture)
end

返回 app/views/users/edit.html.erb:

<% if @character.profilepicture.nil? %>
  <p>Select a picture below to use as your profile picture</p>
<% else %>
  <%= image_tag @character.profilepicture.picture %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails carrierwave


    【解决方案1】:

    解决了。 character.rb 和 picturething.rb 之间的关系没有完全指定。这解决了它:

    字符.rb:

    has_many :standardpictures, class_name: "Picturething",
                                inverse_of: :character,
                                foreign_key: "character_standard_id",
                                dependent: :destroy
    accepts_nested_attributes_for :standardpictures
    
    has_one  :profilepicture,   class_name: "Picturething",
                                inverse_of: :character,
                                foreign_key: "character_profile_id",
                                dependent: :destroy
    

    picturething.rb:

    belongs_to      :character, class_name: "Character",
                                inverse_of: :standardpictures,
                                foreign_key: :character_standard_id
    belongs_to      :character, class_name: "Character",
                                inverse_of: :profilepicture,
                                foreign_key: :character_profile_id
    

    加上迁移在数据库中为 Picturething 添加 :character_standard_id 和 :character_profile_id。

    有趣的是,Picturething 现在需要两列,:character_standard_id 和 :character_profile_id,但每列只包含相同的值。第二列似乎是多余的。我试图让它只使用一个列,但不能。这是由于不可能还是由于我的无能尚不清楚(我怀疑是后者),但很高兴知道:这个问题可以通过 Picturething 数据库中的单个 id 列解决吗?

    【讨论】:

      猜你喜欢
      • 2017-04-25
      • 2016-07-11
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多