Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Created June 15, 2012 02:34
Show Gist options
  • Save jhjguxin/2934404 to your computer and use it in GitHub Desktop.
Save jhjguxin/2934404 to your computer and use it in GitHub Desktop.
rails ajax demo base on BBTangCMS
<%= simple_form_for @knowledge, :html => { :class => 'form-horizontal', :class => "form-inline", :remote => true } do |f| %>
<div class="knowledge-head box">
<span class="label label-info">head</span>
<%= f.input :title %>
<%= f.input :summary, :input_html => { :rows =>"3", :cols =>"50" } %>
<!--%= f.input :content %-->
</div>
<div class="knowledge-content box">
<span class="label label-info">content</span>
<%= f.input :body, :as => :kindeditor, :input_html => { :width => 850, :height => 500 } %>
</div>
<div class="knowledge-tags box" id = "knowledge-tags">
<span class="label label-info">footer</span>
<% unless @identities.empty? %>
<%= f.input :identity_list, :as => :check_boxes, :collection => @identities.uniq{|x| x.name},:checked => (@identities_choose.map(&:id) if @identities_choose), :input_html => { :rows =>"5", :cols =>"20" } %>
<% end %>
<% unless @timelines.empty? %>
<%= f.input :timeline_list, :as => :check_boxes, :collection => @timelines.uniq{|x| x.name},:checked => @timelines_choose.map(&:id), :input_html => { :rows =>"5", :cols =>"20" } %>
<% end %>
<% unless @categories.empty? %>
<%= f.input :category_list, :as => :check_boxes, :collection => @categories.uniq{|x| x.name},:checked => @categories_choose.map(&:id), :input_html => { :rows =>"5", :cols =>"20" } %>
<% end %>
<!--%= link_to "refresh", :action => "refresh", :remote => true %-->
<%= f.button :submit, 'refresh', :class => "btn btn-info", :remote => true, :disable_with => 'loading...' %>
</div>
<div class="knowledge-footer box">
<span class="label label-info">footer</span>
<%= f.input :tag_list,:hint => "请输入标签名,并以英文逗号(',')隔开...", :input_html => { :rows =>"5", :cols =>"20" } %>
<%= f.input :created_name %>
<%= f.input :source_info %>
<!--%= f.input :auto_tags, :input_html => { :rows =>"5", :cols =>"20" } %-->
<%= f.input :created_by, :as => :hidden, :input_html => { :value => current_user.id} %>
<!--%= f.input :updated_by %-->
<!--%= f.input :deleted_at %-->
<!--%= f.input :deleted_by %-->
<!--%= f.input :delta %-->
<!--%= f.input :thanks_count %-->
<!--%= f.input :forwarding_count %-->
<!--%= f.input :comments_count %-->
<!--%= f.input :views_count %-->
</div>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary', :disable_with => 'loading...' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
knowledges_path, :class => 'btn' %>
</div>
<% end %>
$("#<%= dom_id @knowledge %>").html("<%= escape_javascript(render(:partial => 'knowledges/form', locals: { knowledge: @knowledge })) %>");
$("#flashes").replaceWith("<%= j render 'common/flashes'%>");
var editor = KindEditor.create(
'textarea[id="knowledge_body"]',
{
width : '850px',
height: '500px',
uploadJson: '/kindeditor/upload',
fileManagerJson: '/kindeditor/filemanager'
}
);
class KnowledgesController < ApplicationController
load_and_authorize_resource
Model_class = Knowledge.new.class
# GET /knowledges
# GET /knowledges.json
def index
@knowledges = Knowledge.paginate(:page => params[:page], :per_page => 20).order('id DESC')
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), knowledges_path
respond_to do |format|
format.html # index.html.erb
format.json { render json: @knowledges }
end
end
# GET /knowledges/1
# GET /knowledges/1.json
def show
@knowledge = Knowledge.find(params[:id])
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), knowledge_path(@knowledge)
respond_to do |format|
format.html # show.html.erb
format.json { render json: @knowledge }
end
end
# GET /knowledges/new
# GET /knowledges/new.json
def new
@knowledge = Knowledge.new
@identities=Identity.all
@identities_choose=[]
@timelines_choose=[]
@timelines=[]
@categories_choose=[]
@categories=[]
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), new_knowledge_path
respond_to do |format|
format.html # new.html.erb
format.json { render json: @knowledge }
end
end
# GET /knowledges/1/edit
def edit
@knowledge = Knowledge.find(params[:id])
@identities=Identity.all
@identities_choose=[]
@timelines_choose=[]
@timelines=[]
@categories_choose=[]
@categories=[]
unless @knowledge.identities.empty?
@knowledge.identities.each do |identity|
identity = Identity.find_by_name(identity.name)
@identities_choose.push(identity)
end
end
unless @knowledge.timelines.empty?
@knowledge.timelines.each do |timeline|
#timeline = Timeline.find_by_name(timeline.name)
Timeline.where(name:timeline.name).each do |t|
@timelines_choose.push(t) if @identities_choose.include? t.parent
end
end
end
unless @knowledge.categories.empty?
@knowledge.categories.each do |category|
#category = Category.find_by_name(category.name)
Category.where(name:category.name).each do |c|
@categories_choose.push(c) if @timelines_choose.include? c.parent
end
end
end
#####################################
unless @identities_choose.empty?
@identities_choose.each do |identity|
identity = Identity.find_by_name(identity.name)
@timelines += identity.children.all
end
end
unless @timelines_choose.empty?
@timelines_choose.each do |timeline|
#timeline = Timeline.find_by_name(timeline.name)
#@categories += timeline.children.all
Timeline.where(name:timeline.name).each do |t|
@categories += t.children.all if @identities_choose.include? t.parent
end
end
end
#debugger
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), edit_knowledge_path(@knowledge)
end
# POST /knowledges
# POST /knowledges.json
def create
@knowledge = Knowledge.new(params[:knowledge])
@identities=Identity.all
@identities_choose=[]
@timelines_choose=[]
@timelines=[]
@categories_choose=[]
@categories=[]
if params[:knowledge].has_key? "identity_list"
params[:knowledge][:identity_list].each do |identity_id|
if identity_id.present?
@identities_choose.push(Identity.find(identity_id.to_i)) if Identity.find(identity_id.to_i)
end
end
end
if params[:knowledge].has_key? "timeline_list"
params[:knowledge][:timeline_list].each do |timeline_id|
if timeline_id.present?
@timelines_choose.push(Timeline.find(timeline_id.to_i)) if Timeline.find(timeline_id.to_i)
end
end
end
if params[:knowledge].has_key? "category_list"
params[:knowledge][:category_list].each do |category_id|
if category_id.present?
@categories_choose.push(Category.find(category_id.to_i)) if Category.find(category_id.to_i)
end
end
end
#####################################
unless @identities_choose.empty?
@identities_choose.each do |identity|
identity = Identity.find_by_name(identity.name)
@timelines += identity.children.all
end
end
unless @timelines_choose.empty?
@timelines_choose.each do |timeline|
#timeline = Timeline.find_by_name(timeline.name)
#@categories += timeline.children.all
Timeline.where(name:timeline.name).each do |t|
@categories += t.children.all if @identities_choose.include? t.parent
end
end
end
if params[:commit] == 'refresh'
#unless @timelines.empty?
# @timelines.each do |timelines|
# @categories_list += timeline.children.all
# end
#end
#@identities_choose.map!(&:id)
#@timelines_choose.map!(&:id)
#@categories_choose.map!(&:id)
render :refresh
else
@knowledge.identity_list = @identities_choose.map(&:name) if not @identities_choose.empty?
@knowledge.timeline_list = @timelines_choose.map(&:name) if not @timelines_choose.empty?
@knowledge.category_list = @categories_choose.map(&:name) if not @categories_choose.empty?
if @knowledge.save
flash[:notice] = 'Knowledge was successfully updated.'
render js: %[window.location.pathname='#{knowledge_path(@knowledge)}']
#format.html { redirect_to @knowledge, notice: 'Knowledge was successfully created.' }
#format.json { render json: @knowledge, status: :created, location: @knowledge }
else
flash[:notice] = 'Knowledge updated failure.'
respond_to do |format|
format.html { render action: "new" }
format.js { render action: "new" }
format.json { render json: @knowledge.errors, status: :unprocessable_entity }
end
end
end
end
# PUT /knowledges/1
# PUT /knowledges/1.json
def update
@knowledge = Knowledge.find(params[:id])
#debugger
@form_date = params[:knowledge]
@knowledge.from_json @form_date.to_json if @form_date
@identities=Identity.all
@identities_choose= []
@timelines_choose= []
@timelines = []
@categories_choose = []
@categories = []
if params[:knowledge].has_key? "identity_list"
params[:knowledge][:identity_list].each do |identity_id|
if identity_id.present?
@identities_choose.push(Identity.find(identity_id.to_i)) if Identity.find(identity_id.to_i)
end
end
end
if params[:knowledge].has_key? "timeline_list"
params[:knowledge][:timeline_list].each do |timeline_id|
if timeline_id.present?
@timelines_choose.push(Timeline.find(timeline_id.to_i)) if Timeline.find(timeline_id.to_i)
end
end
end
if params[:knowledge].has_key? "category_list"
params[:knowledge][:category_list].each do |category_id|
if category_id.present?
@categories_choose.push(Category.find(category_id.to_i)) if Category.find(category_id.to_i)
end
end
end
#####################################
unless @identities_choose.empty?
@identities_choose.each do |identity|
identity = Identity.find_by_name(identity.name)
@timelines += identity.children.all
end
end
unless @timelines_choose.empty?
@timelines_choose.each do |timeline|
#timeline = Timeline.find_by_name(timeline.name)
#@categories += timeline.children.all
Timeline.where(name:timeline.name).each do |t|
@categories += t.children.all if @identities_choose.include? t.parent
end
end
end
if params[:commit] == 'refresh'
#unless @timelines.empty?
# @timelines.each do |timelines|
# @categories_list += timeline.children.all
# end
#end
#@identities_choose.map!(&:id)
#@timelines_choose.map!(&:id)
#@categories_choose.map!(&:id)
render :refresh
else
params[:knowledge]["identity_list"] = @identities_choose.map(&:name) if not @identities_choose.empty?
params[:knowledge]["timeline_list"] = @timelines_choose.map(&:name) if not @timelines_choose.empty?
params[:knowledge]["category_list"] = @categories_choose.map(&:name) if not @categories_choose.empty?
if @knowledge.update_attributes(params[:knowledge])
#format.html { redirect_to @knowledge, notice: 'Knowledge was successfully updated.' }
#format.json { head :no_content }
flash[:notice] = 'Knowledge was successfully updated.'
render js: %[window.location.pathname='#{knowledge_path(@knowledge)}']
else
flash[:notice] = 'Knowledge updated failure.'
respond_to do |format|
format.html { render action: "edit" }
format.js { render action: "edit" }
format.json { render json: @knowledge.errors, status: :unprocessable_entity }
end
end
end
end
# DELETE /knowledges/1
# DELETE /knowledges/1.json
def destroy
@knowledge = Knowledge.find(params[:id])
@knowledge.destroy
flash[:notice] = 'Knowledge was destroy updated.'
respond_to do |format|
format.html { redirect_to knowledges_url }
format.json { head :no_content }
end
end
end
$("#new_knowledge").html("<%= escape_javascript(render(:partial => 'knowledges/form', locals: { knowledge: @knowledge })) %>");
$("#flashes").replaceWith("<%= j render 'common/flashes'%>");
var editor = KindEditor.create(
'textarea[id="knowledge_body"]',
{
width : '850px',
height: '500px',
uploadJson: '/kindeditor/upload',
fileManagerJson: '/kindeditor/filemanager'
}
);
$("#edit_<%= dom_id @knowledge %>").replaceWith("<%= j render 'form'%>");
$("#new_knowledge").replaceWith("<%= j render 'form'%>");
KindEditor.create(
'textarea[id="knowledge_body"]',
{
width : '850px',
height: '500px',
uploadJson: '/kindeditor/upload',
fileManagerJson: '/kindeditor/filemanager'
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment