我一个人完成了所有工作,起初我的想法并不难,所有技巧都是由JQuery完成的……如果有人遇到和我一样的问题,请分享这个概念
我的模型是
class AppsEvent < ActiveRecord::Base
belongs_to :app
has_many :apps_events_attributes, :foreign_key => 'apps_event_id',
:class_name => 'AppsEventsAttribute',
:dependent => :destroy
accepts_nested_attributes_for :apps_events_attributes,
:reject_if => lambda { |a| (a[:name].blank? && a[:description].blank?) },
:allow_destroy => true
validates_presence_of :description
validates_presence_of :name
validates_presence_of :code
validates_presence_of :apps_events_attributes, :message => "can't be empty"
end
和
class AppsEventsAttribute < ActiveRecord::Base
set_table_name :apps_events_attributes
belongs_to :apps_event, :foreign_key => 'apps_event_id'
attr_accessible :id, :apps_event_id, :name, :description, :attribute_type, :is_std, :created_at, :updated_at
def type
self.attribute_type
end
end
我的控制器是...
class AppsEventsController < ApplicationController
layout :layout_by_resource
before_filter :initialize_default_app
before_filter :check_permission
before_filter :load
def load
@app = current_user.find_app(params[:app_id])
@apps_events = AppsEvent.where(:app_id => @app.id)
@apps_event = AppsEvent.new
end
def index
@app = current_user.find_app(params[:app_id])
@default_app = App.default(current_user)
@apps_events = AppsEvent.where(:app_id => @app.id)
@apps_event_jsons = Hash.new
@apps_events.each do |app_event|
json = Hash.new
json['User_ID'] = 548741213
json['Session_ID'] = 2568639390
json['Action_Type'] = app_event.code
json['eventsData'] = {}
app_event.apps_events_attributes.each do |apps_event_attributes|
if (apps_event_attributes.attribute_type == 'Integer')
json['eventsData'][apps_event_attributes.name] = 1234
elsif (apps_event_attributes.attribute_type == 'Float')
json['eventsData'][apps_event_attributes.name] = 1234.23
else
json['eventsData'][apps_event_attributes.name] = 'abcd'
end
end
@apps_event_jsons[app_event.id] = json
end
end
def new
@apps_event = AppsEvent.new
@app = current_user.find_app(params[:app_id])
@apps_event.app_id = @app.id
@apps_event.apps_events_attributes.build
@action = 'create'
render 'edit'
end
def edit
@app = current_user.find_app(params[:app_id])
@apps_event = AppsEvent.find(params[:id])
@action = 'update'
end
def create
@apps_event = AppsEvent.new(params[:apps_event])
@show_custom_event = 'true'
@apps_event.name = @apps_event.name.strip
respond_to do |format|
if @apps_event.save
format.html {
redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
:notice => "Successfully created #{@apps_event.name} custom definition.")
}
format.js {
flash[:notice] = 'Successfully created event.'
@apps_events = AppsEvent.where(:app_id => @app.id)
}
else
@app = current_user.find_app(params[:app_id])
if (@apps_event.apps_events_attributes == nil || @apps_event.apps_events_attributes.size <= 0)
@apps_event.apps_events_attributes.build
end
@apps_event.populate_code
@action = 'create'
format.html {
redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
:alert => "Error in creating #{@apps_event.name} custom definition.")
}
format.js
end
end
end
def update
@apps_event = AppsEvent.find(params[:apps_event][:id])
params[:apps_event][:name] = params[:apps_event][:name].strip
respond_to do |format|
if @apps_event.update_attributes(params[:apps_event])
format.html {
if(@apps_event.is_std == 'y')
redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
:notice => "Successfully updated #{@apps_event.name} standard definition.")
else
redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
:notice => "Successfully updated #{@apps_event.name} custom definition.")
end
}
format.js {
flash[:notice] = 'Successfully updated event.'
@apps_events = AppsEvent.where(:app_id => @app.id)
render :nothing => true
}
else
@app = current_user.find_app(params[:app_id])
if (@apps_event.apps_events_attributes == nil || @apps_event.apps_events_attributes.size <= 0)
@apps_event.apps_events_attributes.build
end
@apps_event.populate_code
@action = "update"
format.html {
if(@apps_event.is_std == 'y')
redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
:alert => "Error in updating #{@apps_event.name} standard definition.")
else
redirect_to("/app/#{params[:apps_event][:app_id]}/apps_events",
:alert => "Error in updating #{@apps_event.name} custom definition.")
end
}
format.js
end
end
end
def delete
if AppsEvent.delete(params[:id])
redirect_to "/app/#{params[:app_id]}/apps_events", :notice => "Successfully deleted #{@apps_event.name} custom definition."
else
redirect_to "/app/#{params[:app_id]}/apps_events",
:alert => "Error in deleting #{@apps_event.name} custom definition."
end
end
end
我有 5 个视图文件,它们是 index.html.erb、edit.js.erb、_form_custom.html.erb、_form_standard.html.erb 和 _events.html.erb,旁边还有一个用于更新、创建和删除的帮助文件通过设置 remote => true 使用 ajax 调用。在索引文件中,我正在部分渲染所有事件(_events.html.erb),在这里我完成了技巧:P
我的_events.html.erb
<% for apps_event in @apps_events %>
<% if (apps_event.is_std == 'y') %>
<div class="standardEvent showStandard">
<ul>
<li class="column_1"><span style="font-weight: bold;"><%= apps_event.name %></span></li>
<li class="column_2"><span><%= apps_event.code %></span></li>
<li class="column_3"><span><%= apps_event.description %></span></li>
<li class="column_4">
<%= link_to edit_apps_event(apps_event) %>
</li>
<li class="column_5">
</li>
</ul>
</div>
<% else %>
<div class="customEvent showCustom" style="display:none">
<ul>
<li class="column_1"><span style="font-weight: bold;"><%= apps_event.name %></span></li>
<li class="column_2"><span><%= apps_event.code %></span></li>
<li class="column_3"><span><%= apps_event.description %></span></li>
<li class="column_4">
<%= link_to edit_apps_event(apps_event) %>
</li>
<li class="column_5">
<%= remove_apps_event_prompt_link(apps_event) %>
</li>
</ul>
</div>
<% end %>
<div class="clrBoth"></div>
<% end %>
现在你可以弄清楚左边部分的意思——JQuery 部分隐藏或显示一个 div。