Created
July 20, 2012 10:26
-
-
Save jhjguxin/3150046 to your computer and use it in GitHub Desktop.
Rails 3, prototype and multi-select dependencies base on BBTangCMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= collection_select('question', "category_list", categories, :id, :name, | |
{:prompt => "Select a category", :selected => (@question_categories.map(&:id) if @question_categories.present?)}) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= simple_form_for(@question, :html => { :class => "form-inline" }) do |f| %> | |
<%= f.error_notification %> | |
<div class="form-inputs"> | |
<%= f.input :title %> | |
<!--%= f.input :content %--> | |
<%= f.input :body, :as => :kindeditor %> | |
<!--%= f.input :focus_by %--> | |
<%= f.input :score, :as =>:integer %> | |
<!--%= f.input :created_by %--> | |
<!--%= f.input :views_count %--> | |
<!--%= f.input :delta %--> | |
<!--%= f.input :last_answer_time %--> | |
<!--%= f.input :best_answer_id %--> | |
<span class="label label-info">tags</span> | |
<%= f.input :identity_list, :collection => @identities, :selected => (@question_identities.map(&:id) if @question_identities.present?), | |
:input_html => { :rows =>"5", :cols =>"20" , | |
:onchange => "#{remote_function(:url => {:action => 'update_timelines'},:with => "'identity_id='+value")}" | |
} | |
%> | |
<%= f.input :timeline_list do %> | |
<div id="timeline_list"> | |
<%= render :partial => 'timelines', :object => @timelines %> | |
</div> | |
<% end %> | |
<%= f.input :category_list do %> | |
<div id="category_list"> | |
<%= render :partial => 'categories', :object => @categories %> | |
</div> | |
<% end %> | |
<%= f.input :tag_list,:hint => "请输入标签名,并以英文逗号(',')隔开...", :input_html => { :rows =>"5", :cols =>"20" } %> | |
<% if @question.new_record? %> | |
<%= f.input :created_by, :as => :hidden, :input_html => { :value => (current_user.id if current_user.present?) } %> | |
<%= f.input :created_at, :as => :hidden, :input_html => { :value => Time.now } %> | |
<% else %> | |
<%= f.input :updated_at, :as => :hidden, :input_html => { :value => Time.now} %> | |
<% end %> | |
<!--%= f.input :answers_count %--> | |
<!--%= f.input :experts_count %--> | |
<!--%= f.input :designated_answerer %--> | |
<!--%= f.input :auto_tags %--> | |
<!--%= f.input :knowledge_id %--> | |
<%= f.input :is_anonymous %> | |
<%= f.input :soft_deleted, :as => :boolean, :input_html => {:checked => @question.deleted_at? ? "checked" : ""} %> | |
<!--%= f.input :deleted %--> | |
</div> | |
<div class="form-actions"> | |
<%= f.button :submit, :class => 'btn-primary', :disable_with => 'loading...' %> | |
<%= link_to t('.cancel', :default => t("helpers.links.cancel")), | |
questions_path, :class => 'btn' %> | |
</div> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= collection_select("question", "timeline_list", timelines, :id, :name, | |
{:prompt => "Select an timeline", :selected => (@question_timelines.map(&:id) if @question_timelines.present?)}, | |
{:onchange => "#{remote_function(:url => {:action => "update_categories"}, | |
:with => "'timeline_id='+value")}"}) %> | |
<!--%= grouped_collection_select("question", "timeline_list", timelines, :id, :name, | |
{:prompt => "Select an timeline"}, | |
{:onchange => "#{remote_function(:url => {:action => "update_categories"}, | |
:with => "'timeline_id='+value")}"})%--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//= require jquery | |
//= require jquery_ujs | |
//= require jquery-ui | |
//= require prototype | |
// require prototype_ujs /* in my case it conflict with jquery_ujs | |
//= require effects | |
//= require dragdrop | |
// require controls | |
//= require twitter/bootstrap | |
//= require kindeditor | |
//= require_tree . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "prototype-rails", "~> 3.2.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuestionsController < ApplicationController | |
load_and_authorize_resource | |
Model_class = Question.new.class | |
# GET /questions | |
# GET /questions.json | |
def index | |
@questions = Question.paginate(:page => params[:page]).order('score DESC') | |
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), questions_path | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @questions } | |
end | |
end | |
# GET /questions/1 | |
# GET /questions/1.json | |
def show | |
@question = Question.find(params[:id]) | |
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), question_path(@question) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @question } | |
end | |
end | |
# GET /questions/new | |
# GET /questions/new.json | |
def new | |
@question = Question.new | |
@identities = Identity.all | |
@timelines = Timeline.all | |
@categories = Category.all | |
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), new_question_path | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @question } | |
end | |
end | |
# GET /questions/1/edit | |
def edit | |
@question = Question.find(params[:id]) | |
@identities = Identity.all | |
@timelines = [] | |
@categories = [] | |
@question_identities = [] | |
if @question.identities.present? | |
@question.identities.each do |identity| | |
identity = Identity.find_by_name(identity.name) | |
@question_identities.push(identity) | |
end | |
end | |
@question_timelines = [] | |
@question.timelines.each do |timeline| | |
#timeline = Timeline.find_by_name(timeline.name) | |
Timeline.where(name: timeline.name).each do |t| | |
@question_timelines.push(t) if @question_identities.include? t.parent | |
end | |
end | |
@question_categories = [] | |
@question.categories.each do |category| | |
#category = Category.find_by_name(category.name) | |
Category.where(name: category.name).each do |c| | |
@question_categories.push(c) if @question_timelines.include? c.parent | |
end | |
end | |
if @question_identities.present? | |
@question_identities.each do |identity| | |
identity = Identity.find_by_name(identity.name) | |
@timelines += identity.children.all if identity.present? | |
end | |
end | |
if @question_timelines.present? | |
@question_timelines.each do |timeline| | |
Timeline.where(name: timeline.name).each do |t| | |
@categories += t.children.all if @question_identities.include? t.parent | |
end | |
end | |
end | |
breadcrumbs.add I18n.t("helpers.titles.#{current_action}", :model => Model_class.model_name.human), edit_question_path | |
end | |
# POST /questions | |
# POST /questions.json | |
def create | |
@question = Question.new(params[:question]) | |
@question.if_soft_deleted(params[:question][:soft_deleted],current_user) if params[:question][:soft_deleted].present? | |
#params[:question][:identity_list].collect!{|i| Identity.find(i) if Identity.exists? i}.compact! if params[:question][:identity_list].present? | |
#params[:question][:timeline_list].collect!{|t| Timeline.find(t) if Timeline.exists? t}.compact! if params[:question][:timeline_list].present? | |
#params[:question][:category_list].collect!{|c| Category.find(c) if Category.exists? c}.compact! if params[:question][:category_list].present? | |
params[:question][:identity_list] = Identity.find(params[:question][:identity_list]) if Identity.exists? params[:question][:identity_list] if params[:question][:identity_list].present? | |
params[:question][:timeline_list] = Timeline.find(params[:question][:timeline_list]) if Timeline.exists? params[:question][:timeline_list] if params[:question][:timeline_list].present? | |
params[:question][:category_list] = Category.find(params[:question][:category_list]) if Category.exists? params[:question][:category_list] if params[:question][:category_list].present? | |
respond_to do |format| | |
if @question.save | |
format.html { redirect_to @question, notice: 'Question was successfully created.' } | |
format.json { render json: @question, status: :created, location: @question } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @question.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /questions/1 | |
# PUT /questions/1.json | |
def update | |
@question = Question.find(params[:id]) | |
#params[:question][:identity_list].collect!{|i| Identity.find(i) if Identity.exists? i}.compact! if params[:question][:identity_list].present? | |
#params[:question][:timeline_list].collect!{|t| Timeline.find(t) if Timeline.exists? t}.compact! if params[:question][:timeline_list].present? | |
#params[:question][:category_list].collect!{|c| Category.find(c) if Category.exists? c}.compact! if params[:question][:category_list].present? | |
params[:question][:identity_list] = Identity.find(params[:question][:identity_list]) if Identity.exists? params[:question][:identity_list] if params[:question][:identity_list].present? | |
params[:question][:timeline_list] = Timeline.find(params[:question][:timeline_list]) if Timeline.exists? params[:question][:timeline_list] if params[:question][:timeline_list].present? | |
params[:question][:category_list] = Category.find(params[:question][:category_list]) if Category.exists? params[:question][:category_list] if params[:question][:category_list].present? | |
@question.if_soft_deleted(params[:question][:soft_deleted],current_user) if params[:question][:soft_deleted].present? | |
respond_to do |format| | |
if @question.update_attributes(params[:question]) | |
format.html { redirect_to @question, notice: 'Question was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @question.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /questions/1 | |
# DELETE /questions/1.json | |
def destroy | |
@question = Question.find(params[:id]) | |
@question.destroy | |
respond_to do |format| | |
format.html { redirect_to questions_url } | |
format.json { head :no_content } | |
end | |
end | |
#question_list = [1,2,3] | |
def resetscore | |
if params.has_key? :question_list | |
question_list = params[:question_list] | |
select_questions = Question.where id: question_list | |
else | |
select_questions = Question.all | |
end | |
select_questions.each do |question| | |
question.score = 0 | |
question.save | |
end | |
redirect_to questions_url | |
end | |
def update_timelines | |
# updates identity and timelines based on genre selected | |
identity = Identity.find(params[:identity_id]) | |
timelines = identity.children.all | |
categories = [] | |
render :update do |page| | |
page.replace_html 'timeline_list', :partial => 'timelines', :object => timelines | |
page.replace_html 'category_list', :partial => 'categories', :object => categories | |
end | |
end | |
def update_categories | |
# updates categories based on timeline selected | |
timeline = Timeline.find(params[:timeline_id]) | |
categories = timeline.children.all | |
render :update do |page| | |
page.replace_html 'category_list', :partial => 'categories', :object => categories | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :questions do | |
collection do | |
get 'resetscore' | |
match 'update_timelines' | |
match 'update_categories' | |
end | |
resources :answers | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MarkUsProject/Markus#603
http://stackoverflow.com/questions/3332474/how-to-make-ajax-calls-with-rails-3-using-remote-function
In rails 3.1 and onward, its preferred to use unobstructive javascript instead of remote_function calls. The call to remote_function just generates an Ajax request so it can be replaced by an inline Ajax request. These calls occur in many places across the code base, so someone could look into changing all occurrences at once.
* so I will rewrite the demo with jQuery ...*