ADD: Dateieinbindung in TinyMCE für Attachments

This commit is contained in:
HausdorffHimself
2013-08-24 04:07:33 +02:00
parent adbbce5fc8
commit 94614b1d5b
11 changed files with 79 additions and 18 deletions

View File

@@ -21,3 +21,19 @@
//= require bootstrap/load-image.min //= require bootstrap/load-image.min
//= require bootstrap/image-gallery.min //= require bootstrap/image-gallery.min
//= require jquery-fileupload //= require jquery-fileupload
function insertAttachment(url,name) {
var ext = url.split('.').pop();
var img_ext = [ "jpg", "png", "bmp" , "jpeg" ];
var text_ext = [ "pdf" ];
if ( img_ext.indexOf(ext) > -1) {
tinymce.activeEditor.setContent(tinymce.activeEditor.getContent({format : 'raw'}) + "<img src=\"" + url + "\" title=\"" + name + "\">");
}
else if (text_ext.indexOf(ext) > -1) {
tinymce.activeEditor.setContent(tinymce.activeEditor.getContent({format : 'raw'}) + "<a href=\"" + url + "\">" + name +"</a>");
}
else {
alert("<a href=\"" + url + "\">" + "Link</a>");
}
}

View File

@@ -25,7 +25,9 @@ class AttachmentsController < ApplicationController
# GET /attachments/new.json # GET /attachments/new.json
def new def new
@attachment = Attachment.new @attachment = Attachment.new
@thema = Thema.find(params[:thema_id])
@attachment.thema = @thema
respond_to do |format| respond_to do |format|
format.html # new.html.erb format.html # new.html.erb
format.json { render json: @attachment } format.json { render json: @attachment }
@@ -35,17 +37,20 @@ class AttachmentsController < ApplicationController
# GET /attachments/1/edit # GET /attachments/1/edit
def edit def edit
@attachment = Attachment.find(params[:id]) @attachment = Attachment.find(params[:id])
@thema = @attachment.thema
end end
# POST /attachments # POST /attachments
# POST /attachments.json # POST /attachments.json
def create def create
@attachment = Attachment.new(params[:attachment]) @attachment = Attachment.new(params[:attachment])
@thema = Thema.find(params[:thema_id])
@attachment.thema_id = @thema.id
respond_to do |format| respond_to do |format|
if @attachment.save if @attachment.save
format.html { redirect_to @attachment, notice: 'Attachment was successfully created.' } format.html { redirect_to @thema, notice: 'Attachment was successfully created.' }
format.json { render json: @attachment, status: :created, location: @attachment } format.json { render json: @thema, status: :created, location: @thema }
else else
format.html { render action: "new" } format.html { render action: "new" }
format.json { render json: @attachment.errors, status: :unprocessable_entity } format.json { render json: @attachment.errors, status: :unprocessable_entity }
@@ -57,10 +62,11 @@ class AttachmentsController < ApplicationController
# PUT /attachments/1.json # PUT /attachments/1.json
def update def update
@attachment = Attachment.find(params[:id]) @attachment = Attachment.find(params[:id])
@thema = @attachment.thema
respond_to do |format| respond_to do |format|
if @attachment.update_attributes(params[:attachment]) if @attachment.update_attributes(params[:attachment])
format.html { redirect_to @attachment, notice: 'Attachment was successfully updated.' } format.html { redirect_to @thema, notice: 'Attachment was successfully updated.' }
format.json { head :no_content } format.json { head :no_content }
else else
format.html { render action: "edit" } format.html { render action: "edit" }
@@ -74,9 +80,10 @@ class AttachmentsController < ApplicationController
def destroy def destroy
@attachment = Attachment.find(params[:id]) @attachment = Attachment.find(params[:id])
@attachment.destroy @attachment.destroy
@thema = Thema.find(params[:thema_id])
respond_to do |format| respond_to do |format|
format.html { redirect_to attachments_url } format.html { redirect_to @thema }
format.json { head :no_content } format.json { head :no_content }
end end
end end

View File

@@ -4,6 +4,7 @@
# #
# id :integer not null, primary key # id :integer not null, primary key
# name :string(255) # name :string(255)
# datei :string(255)
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# thema_id :integer # thema_id :integer
@@ -11,9 +12,9 @@
class Attachment < ActiveRecord::Base class Attachment < ActiveRecord::Base
has_paper_trail has_paper_trail
attr_accessible :name attr_accessible :name, :datei
belongs_to :thema belongs_to :thema
mount_uploader :datei, BeispieldateiUploader
validates :thema, :presence => true validates :thema, :presence => true
validates :name, :presence => true validates :name, :presence => true
end end

View File

@@ -7,7 +7,6 @@
# text :text # text :text
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
#
class Themengruppe < ActiveRecord::Base class Themengruppe < ActiveRecord::Base
WORD_COUNT = 50 WORD_COUNT = 50

View File

@@ -1,6 +1,7 @@
<%= semantic_form_for @attachment do |f| %> <%= semantic_form_for [@thema,@attachment] do |f| %>
<%= f.inputs do %> <%= f.inputs do %>
<%= f.input :name %> <%= f.input :name %>
<%= f.input :datei, :as => :file %>
<% end %> <% end %>
<%= f.actions do %> <%= f.actions do %>

View File

@@ -2,5 +2,5 @@
<%= render 'form' %> <%= render 'form' %>
<%= link_to 'Show', @attachment %> |
<%= link_to 'Back', attachments_path %> <%= link_to 'Back', thema_attachments_path(@attachment.thema,@attachment) %>

View File

@@ -2,4 +2,4 @@
<%= render 'form' %> <%= render 'form' %>
<%= link_to 'Back', attachments_path %> <%= link_to 'Back', @thema %>

View File

@@ -8,7 +8,31 @@
<% end %> <% end %>
<%= tinymce %> <%= tinymce %>
<h2>Attachments:</h2>
<% @thema.attachments.each do |attachment| %>
<ul>
<li>
<%= link_to attachment.name, edit_thema_attachment_path(@thema,attachment) %>
<button type="button" onclick="insertAttachment(<%="\"" + attachment.datei.url + "\""%>,<%="\""+attachment.name+"\""%>)">Insert Me!</button>
</li>
</ul>
<% end %>
<%= f.actions do %> <%= f.actions do %>
<%= f.action :submit, :as => :input %> <%= f.action :submit, :as => :input %>
<% end %> <% end %>
<% end %> <% end %>
<!--
<script>
function insertAttachment(url) {
tinymce.activeEditor.setContent(tinymce.activeEditor.getContent({format : 'raw'}) + "Test");
//editor.setContent(editor.getContent + "<img src=\"" + url + "\">")
}
</script>
-->

View File

@@ -20,6 +20,16 @@
</p> </p>
<% end %> <% end %>
<%= link_to 'Neue Frage', new_frage_path %> <br/>
<br/> <br/>
<%= link_to 'Neue Frage', new_frage_path %> <h2>Attachments:</h2>
<% @thema.attachments.each do |attachment| %>
<ul>
<li>
<%= link_to attachment.name, edit_thema_attachment_path(@thema,attachment) %> | <%= link_to 'Destroy',[@thema,attachment], method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
</ul>
<% end %>
<%= link_to 'Neues Attachment', new_thema_attachment_path(@thema) %>
<br/>

View File

@@ -66,8 +66,10 @@
resources :beispiele resources :beispiele
resources :attachment resources :themen do
resources :themen resources :attachments
end
resources :themengruppen do resources :themengruppen do
resources :themen, :only=>[:new, :show] resources :themen, :only=>[:new, :show]
end end

View File

@@ -7,4 +7,5 @@ theme_advanced_buttons3_add:
- fullscreen - fullscreen
plugins: plugins:
- table - table
- fullscreen - fullscreen
- advimage