Skip to content

Commit

Permalink
admin edit tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0425 committed Jul 25, 2012
1 parent fde2f49 commit e48c285
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 73 deletions.
65 changes: 19 additions & 46 deletions app/controllers/admin/tags_controller.rb
Expand Up @@ -4,88 +4,61 @@ class Admin::TagsController < Admin::BaseController
Cls = 'n2'

# GET /tags
# GET /tags.json
def index
@cls = Cls
@tags = Tag.page(params[:page] || 1).per(Pagesize)

respond_to do |format|
format.html # index.html.erb
format.json { render :json => @tags }
end
end

# GET /tags/1
# GET /tags/1.json
def show
@cls = Cls
@tag = Tag.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render :json => @tag }
end
end

# GET /tags/new
# GET /tags/new.json
def new
@cls = Cls
@title = "添加标签"
@tag = Tag.new
end

# POST /tags
def create
@tag = Tag.new(params[:tag])

respond_to do |format|
format.html # new.html.erb
format.json { render :json => @tag }
if @tag.save
flash[:notice] = flash_success()
redirect_to admin_tags_path
else
render :action => "new"
end
end

# GET /tags/1/edit
def edit
@cls = Cls
@title = "修改标签"
@tag = Tag.find(params[:id])
end

# POST /tags
# POST /tags.json
def create
@tag = Tag.new(params[:tag])

respond_to do |format|
if @tag.save
format.html { redirect_to @tag, :notice => 'Tag was successfully created.' }
format.json { render :json => @tag, :status => :created, :location => @tag }
else
format.html { render :action => "new" }
format.json { render :json => @tag.errors, :status => :unprocessable_entity }
end
end
render :new
end

# PUT /tags/1
# PUT /tags/1.json
def update
@tag = Tag.find(params[:id])

respond_to do |format|
if @tag.update_attributes(params[:tag])
format.html { redirect_to @tag, :notice => 'Tag was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @tag.errors, :status => :unprocessable_entity }
end
if @tag.update_attributes(params[:tag])
flash[:notice] = flash_success()
redirect_to admin_tags_path
else
render :action => "edit"
end
end

# DELETE /tags/1
# DELETE /tags/1.json
def destroy
@tag = Tag.find(params[:id])
@tag.destroy

respond_to do |format|
format.html { redirect_to tags_url }
format.json { head :no_content }
end
redirect_to tags_url
end
end
20 changes: 19 additions & 1 deletion app/models/tag.rb
Expand Up @@ -3,8 +3,26 @@ class Tag
include Mongoid::Document
has_and_belongs_to_many :plants
field :name, :type => String
field :usage, :type => Integer
field :usage, :type => Integer, :default => 0
field :description, :type => String
field :icon, :type => String # 标签图标

index :name

# usage
Normal = 0 # 普通标签
Category = 1 # 类型
Sharp = 2 # 花型
Color = 3 # 花色

def self.usages
[["普通", Normal], ["类型", Category], ["花型", Sharp], ["花色", Color]]
end

def usage_name
ua = self.class.usages.select{|u| u[1] == self.usage}
return nil if ua.blank?
return ua[0][0]
end

end
22 changes: 0 additions & 22 deletions app/uploaders/avatar_uploader.rb
Expand Up @@ -39,27 +39,5 @@ def store_dir
end

version :original



# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end


# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end

end
19 changes: 16 additions & 3 deletions app/views/admin/tags/_form.haml
@@ -1,10 +1,23 @@
= form_for @tag do |f|
= form_for [:admin, @tag] do |f|
-if @tag.errors.any?
#error_explanation
%h2= "#{pluralize(@tag.errors.count, "error")} prohibited this tag from being saved:"
%ul
- @tag.errors.full_messages.each do |msg|
%li= msg

.actions
= f.submit 'Save'
%dl.form
%dt= f.label :name, "名称:"
%dd= f.text_field :name, :required => "required"
%p.tips

%dt= f.label :usage, "类型:"
%dd= f.select :usage, options_for_select(Tag.usages, @tag.usage)
%p.tips

%dt= f.label :description, "描述:"
%dd= f.text_area :description
%p.tips

%dt.submit
= f.submit '保 存'
2 changes: 1 addition & 1 deletion app/views/admin/tags/index.haml
Expand Up @@ -13,7 +13,7 @@
%td
= link_to tag.name, tag, :target => "_blank"
%td
某类型
= tag.usage_name
%td
= link_to '编辑', edit_admin_tag_path(tag)
Expand Down

0 comments on commit e48c285

Please sign in to comment.