Skip to content

Instantly share code, notes, and snippets.

@powerwlsl
Created March 21, 2017 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save powerwlsl/25d89baf5c9ba2f4f03a877a14742ebd to your computer and use it in GitHub Desktop.
Save powerwlsl/25d89baf5c9ba2f4f03a877a14742ebd to your computer and use it in GitHub Desktop.
<div class="row">
<div class="col-lg-12">
<div class="panel">
<header class="panel-heading lead">고객 사진 추가</header>
<div class="panel-body">
<%= simple_form_for( [:staffs, @user], url: staffs_user_photo_path, html: { class: 'form-horizontal', multipart: true } ) do |f| %>
<% if @user.errors.present? %>
<script type="text/javascript">
<% @user.errors.full_messages.each do |msg| %>
toastr.error('<%= msg %>');
<% end %>
</script>
<% end %>
<div class="form-group">
<%= f.file_field :files, accept: 'image/*', multiple: true, onChange: 'handleFileSelect()' %>
<output class="row">
<div class="col-md-3" id="result"></div>
</output>
<%= image_tag '', class: ' avatar_preview' %>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<%= f.submit class: 'btn btn-info' %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<script>
function handleFileSelect(e) {
//Check File API support
if (window.File && window.FileList && window.FileReader) {
var files = event.target.files; //FileList object
var output = document.getElementById("result");
// var div = document.createElement("div").classList.add("col-md-3");
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file.type.match('image')) continue;
var picReader = new FileReader();
picReader.addEventListener("load", function (event) {
var picFile = event.target;
var aTag = document.createElement("a");
aTag.setAttribute("href", "#");
aTag.classList.add("thumbnail");
aTag.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" + "title='" + picFile.name + "'/>";
output.insertBefore(aTag, null);
});
//Read the image
picReader.readAsDataURL(file);
}
} else {
console.log("Your browser does not support File API");
}
}
</script>
class User < ApplicationRecord
extend Enumerize
belongs_to :group, optional: true
has_one :measured_value, -> { order(created_at: :desc) }
has_one :body_shape, -> { order(created_at: :desc) }
has_many :orders
has_many :user_attachments
has_many :staff_user_managements
has_many :staffs, through: :staff_user_managements
accepts_nested_attributes_for :user_attachments
validates :name, :phone, :email, presence: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enumerize :age, in: {'20' => 20, '30' => 30, '40' => 40, '50' => 50}
def consultant_result
{
estimate_suit_price: 1_000_000,
estimate_host_suit_price: 1_000_000,
result: '',
etc: ''
}
end
def save_attachments(params)
params[:files].each do |file|
self.user_attachments.create(:file => file)
end
end
def style_survey_count
return false if style_survey.nil?
style_survey.values.select{ |x| x == 'applicable' }.count
end
def style_survey_result
return false if style_survey.nil?
version = style_survey.to_a.last.first[1..8]
SurveyQuestion.new(version).result(style_survey_count)
end
end
class Staffs::UserPhotosController < StaffsController
def edit
@user = User.find_by_id(params[:id])
@user.user_attachments.build
end
def update
user = User.find_by_id(params[:id])
raise user_params
if user.save_attachments(user_params)
redirect_to edit_staffs_user_referrer_path(user), notice: '소중한 고객 정보가 수정되었습니다.'
else
render :edit
end
end
private
def user_params
params.require(:user).permit(files: [])
end
end
class User < ApplicationRecord
extend Enumerize
belongs_to :group, optional: true
has_one :measured_value, -> { order(created_at: :desc) }
has_one :body_shape, -> { order(created_at: :desc) }
has_many :orders
has_many :user_attachments
has_many :staff_user_managements
has_many :staffs, through: :staff_user_managements
accepts_nested_attributes_for :user_attachments
validates :name, :phone, :email, presence: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enumerize :age, in: {'20' => 20, '30' => 30, '40' => 40, '50' => 50}
def consultant_result
{
estimate_suit_price: 1_000_000,
estimate_host_suit_price: 1_000_000,
result: '',
etc: ''
}
end
def save_attachments(params)
params[:files].each do |file|
self.user_attachments.create(:file => file)
end
end
def style_survey_count
return false if style_survey.nil?
style_survey.values.select{ |x| x == 'applicable' }.count
end
def style_survey_result
return false if style_survey.nil?
version = style_survey.to_a.last.first[1..8]
SurveyQuestion.new(version).result(style_survey_count)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment