Ruby on Rails
Thursday, July 9, 2015
Each User has_one Character. Each Character has_one :profilepicture, of class Picturething, which holds a Carrierwave mount_uploader to upload a singe picture. Each Character has_many :standardpictures, also of class Picturething. Picture upload is handled in views/users/edit, which hits the update_pictures method in users_controller.
The idea is to upload one standardpicture at a time. It seems to work, Rails console > Picturething.all shows that a new Picturething has been added to the database, and it is correctly displayed on the page. This is intended to be one of the character.standardpictures.
The weird thing is, somehow in this whole process, the character's :profilepicture is also set to be the same picture that was uploaded. I don't understand how this is happening. At no point do I have code saying something like "@character.profilepicture = standardpicture", but somehow it has decided that both the first :standardpicture and the :profilepicture are one and the same. If the profilepicture exists, which it shouldn't yet, it is displayed on the edit.html.erb page, where I have the line `<% if @character.profilepicture.nil? %>`. It displays the uploaded picture here, so clearly profilepicture is not nil, but it should be.
How is this happening?
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
endapp/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_userusers_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)
endBack to 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 %>You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e2eb9e8d-8768-42f5-b985-13dae4e1bcf5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment