Fix edit and update actions

This commit is contained in:
Senén Rodero Rodríguez
2017-08-14 15:39:58 +02:00
parent e9c5f77368
commit f226d2ac94
6 changed files with 17 additions and 9 deletions

View File

@@ -30,7 +30,7 @@ App.Documentable =
if result.status == 200
$(data.progressBar).find('.loading-bar').removeClass 'uploading'
$(data.progressBar).find('.loading-bar').addClass 'complete'
inputId = '#' + $(e.target).data('chached-attachment-input-field')
inputId = '#' + $(e.target).data('cached-attachment-input-field')
$(inputId).val result.attachment
else
$(data.progressBar).find('.loading-bar').addClass 'errors'

View File

@@ -55,11 +55,12 @@ module CommentableActions
end
def edit
prepare_edit_resource_documents(resource)
end
def update
resource.assign_attributes(strong_params)
resource = parse_documents(resource)
parse_documents(resource)
if resource.save
redirect_to resource, notice: t("flash.actions.update.#{resource_name.underscore}")
else
@@ -114,12 +115,20 @@ module CommentableActions
def prepare_new_resource_documents
if @resource.class == Proposal || @resource.class == Budget::Investment
(0..@resource.class.max_documents_allowed - 1).each do
(1..@resource.class.max_documents_allowed).each do
@resource.documents.build
end
end
end
def prepare_edit_resource_documents(resource)
if resource.class == Proposal || resource.class == Budget::Investment
(resource.documents.count + 1 .. resource.class.max_documents_allowed).each do
resource.documents.build
end
end
end
def parse_documents(resource)
resource.documents.each do |document|
document.user = current_user
@@ -127,7 +136,6 @@ module CommentableActions
resource.documents = resource.documents.select{|document| document.valid? }.each do |document|
document.attachment = File.open(document.cached_attachment)
end
resource
end
end

View File

@@ -25,7 +25,7 @@ class ProposalsController < ApplicationController
def create
@proposal = Proposal.new(proposal_params.merge(author: current_user))
@proposal = parse_documents(@proposal)
parse_documents(@proposal)
if @proposal.save
redirect_to share_proposal_path(@proposal), notice: I18n.t('flash.actions.create.proposal')
@@ -78,7 +78,7 @@ class ProposalsController < ApplicationController
def proposal_params
params.require(:proposal).permit(:title, :question, :summary, :description, :external_url, :video_url,
:responsible_name, :tag_list, :terms_of_service, :geozone_id,
documents_attributes: [:cached_attachment, :title] )
documents_attributes: [:id, :title, :attachment, :cached_attachment ] )
end
def retired_params

View File

@@ -13,7 +13,7 @@ class Proposal < ActiveRecord::Base
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
accepts_nested_attributes_for :documents
accepts_nested_attributes_for :documents, allow_destroy: true
acts_as_votable
acts_as_paranoid column: :hidden_at

View File

@@ -23,7 +23,7 @@
class: 'document_ajax_attachment show-for-sr',
data: {
url: upload_documents_url(documentable_type: @document.documentable_type, documentable_id: @document.documentable_id),
chached_attachment_input_field: "document_cached_attachment",
cached_attachment_input_field: "document_cached_attachment",
multiple: false
} %>
<%= f.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>

View File

@@ -64,7 +64,7 @@
documentable_type: document_fields.object.documentable_type,
documentable_id: document_fields.object.documentable_id
),
chached_attachment_input_field: "proposal_documents_attributes_#{index}_cached_attachment",
cached_attachment_input_field: "proposal_documents_attributes_#{index}_cached_attachment",
multiple: false
} %>
<%= document_fields.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>