每个用户has_one角色。每个角色has_one:profilepicture,类Picturething,它包含Carrierwave mount_uploader以上传单个图片。每个角色has_many:standardpictures,也是Picture 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不是零,但它应该是。
这是怎么回事?
character.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
应用程序/视图/用户/ 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 %>
答案 0 :(得分:0)
解决。 character.rb和picturething.rb之间的关系没有完全指定。这解决了它:
character.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
再加上要添加的迁移:character_standard_id和:数据库中Picturething的character_profile_id。
有趣的是,Picturething现在需要两列:character_standard_id和:character_profile_id,但每列只包含相同的值。第二栏似乎是多余的。我试图让它只使用一个列,但不能。这是由于它是不可能的还是由于我的无能不明确(我怀疑是后者),但是很高兴知道:可以用Picturething数据库中的单个id列来解决这个问题吗?