add co-author db and curd page, but it can't destory it...
This commit is contained in:
		
							parent
							
								
									ccfecb8243
								
							
						
					
					
						commit
						95fc5676bf
					
				|  | @ -0,0 +1,71 @@ | ||||||
|  | class Desktop::CoAuthorsController < ApplicationController | ||||||
|  |   def index | ||||||
|  |     @co_authors = CoAuthor.where(name_id: current_user.id) | ||||||
|  | 
 | ||||||
|  |     respond_to do |format| | ||||||
|  |       format.html { render :layout => false} | ||||||
|  |       format.json { render json: @co_authors } | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def show | ||||||
|  |     @co_author = CoAuthor.find(params[:id]) | ||||||
|  | 
 | ||||||
|  |     respond_to do |format| | ||||||
|  |       format.html { redirect_to desktop_co_authors_url, :layout => false } | ||||||
|  |       format.json { render json: @co_author } | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def new | ||||||
|  |     @co_author = CoAuthor.new | ||||||
|  | 
 | ||||||
|  |     respond_to do |format| | ||||||
|  |       format.html { render :layout => false} | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def edit | ||||||
|  |     @co_author = CoAuthor.find(params[:id]) | ||||||
|  |     respond_to do |format| | ||||||
|  |       format.html { render :layout => false} | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def create | ||||||
|  |     @co_author = CoAuthor.new(params[:co_author]) | ||||||
|  |     @co_author.name_id= current_user.id | ||||||
|  | 
 | ||||||
|  |     respond_to do |format| | ||||||
|  |       if @co_author.save | ||||||
|  |         format.html { redirect_to desktop_co_authors_path, :layout => false, notice: 'CoAuthor was successfully created.' } | ||||||
|  |       else | ||||||
|  |         format.html { render action: "new", :layout => false} | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def update | ||||||
|  |     @co_author = CoAuthor.find(params[:id]) | ||||||
|  | 
 | ||||||
|  |       respond_to do |format| | ||||||
|  |         if @co_author.update_attributes(params[:co_author]) | ||||||
|  |           format.html { redirect_to desktop_co_authors_url, notice: 'CoAuthor was successfully updated.' } | ||||||
|  |           format.json { head :no_content } | ||||||
|  |         else | ||||||
|  |           format.html { render action: "edit" } | ||||||
|  |           format.json { render json: @co_author.errors, status: :unprocessable_entity } | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def delete | ||||||
|  |     @co_author = CoAuthor.find(params[:id]) | ||||||
|  |     @co_author.destroy | ||||||
|  | 
 | ||||||
|  |     respond_to do |format| | ||||||
|  |       format.html { redirect_to desktop_co_authors_url, :layout => false } | ||||||
|  |       format.json { head :no_content } | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | @ -0,0 +1,19 @@ | ||||||
|  | class CoAuthor | ||||||
|  |   include Mongoid::Document | ||||||
|  | 
 | ||||||
|  |   LANGUAGE_TYPES = [ "English", "Chinese" ] | ||||||
|  | 
 | ||||||
|  |   field :name_id, type: BSON::ObjectId | ||||||
|  |   field :co_author, localize: true | ||||||
|  |   field :email | ||||||
|  |   field :type | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | ||||||
|  |   validates :email, format: { with: VALID_EMAIL_REGEX }, | ||||||
|  |              uniqueness: { case_sensitive: false } | ||||||
|  | 
 | ||||||
|  |   validates :co_author, presence: true | ||||||
|  |   before_save { |coauthor| coauthor.email = email.downcase } | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,44 @@ | ||||||
|  | <% if @co_author.errors.any? %> | ||||||
|  |   <div id="error_explanation"> | ||||||
|  |     <h2><%= pluralize(@co_author.errors.count, "error") %> prohibited this user from being saved:</h2> | ||||||
|  |     <ul> | ||||||
|  |       <% @co_author.errors.full_messages.each do |msg| %> | ||||||
|  |         <li><%= msg %></li> | ||||||
|  |       <% end %> | ||||||
|  |     </ul> | ||||||
|  |   </div> | ||||||
|  | <% end %> | ||||||
|  | <div class="fn_g hp"> | ||||||
|  | 
 | ||||||
|  |   <%= button_tag "Save", name: "commit", value: "Save", class: "fn_btn hh2 thmc2 thmtxt" %> | ||||||
|  |   <%= button_tag "Cancel", name: "commit", value: "Cancel", class: "fn_btn hh2 thmc2 thmtxt" %> | ||||||
|  | </div> | ||||||
|  | 
 | ||||||
|  | <div></div> | ||||||
|  | <ul> | ||||||
|  |   <li class="s_grid_row"> | ||||||
|  |   <%= f.label :co_author %><br /> | ||||||
|  |   <%= f.text_field :co_author %> | ||||||
|  |   </li> | ||||||
|  | 
 | ||||||
|  |   <li class="s_grid_row"> | ||||||
|  |   <%= f.label :email %><br /> | ||||||
|  |   <%= f.text_field :email %> | ||||||
|  |   </li> | ||||||
|  | 
 | ||||||
|  |   <li class="s_grid_row"> | ||||||
|  |   <%= f.label :type%><br /> | ||||||
|  |   <select name="co_author[type]" id="" class="s_grid s_grid_4"> | ||||||
|  |     <%= options_for_select( | ||||||
|  |       [["朋友","朋友"], | ||||||
|  |        ["學生","摯友"], | ||||||
|  |        ["老師","老師"], | ||||||
|  |        ["摯友","摯友"], | ||||||
|  |        ["親戚","親戚"], | ||||||
|  |        ["同事","同事"], | ||||||
|  |        ["共同作者","共同作者"] | ||||||
|  |     ]) | ||||||
|  |   %> | ||||||
|  |   </select> | ||||||
|  |   </li> | ||||||
|  | </ul> | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | <%= form_for @co_author, url: desktop_co_author_path(@co_author) do |f| %> | ||||||
|  |   <%= render partial: 'desktop/co_authors/form', locals: {:f => f} %> | ||||||
|  | <% end %> | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | <table> | ||||||
|  |   <tr> | ||||||
|  |     <th>Name</th> | ||||||
|  |     <th>EMail</th> | ||||||
|  |     <th>Type</th> | ||||||
|  |     <th></th> | ||||||
|  |     <th></th> | ||||||
|  |     <th></th> | ||||||
|  |   </tr> | ||||||
|  | 
 | ||||||
|  | <% @co_authors.each do |co_author| %> | ||||||
|  |   <tr> | ||||||
|  |     <td><%= co_author.co_author %></td> | ||||||
|  |     <td><%= co_author.email %></td> | ||||||
|  |     <td><%= co_author.type%></td> | ||||||
|  |     <td><%= link_to 'Destroy', desktop_co_author_path(co_author), method: :delete, confirm: 'Are you sure?'  %></td> | ||||||
|  |   </tr> | ||||||
|  | <% end %> | ||||||
|  | </table> | ||||||
|  | <br /> | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | <%= form_for @co_author, url: desktop_co_authors_path do |f| %> | ||||||
|  |   <%= render partial: 'desktop/co_authors/form', locals: {:f => f} %> | ||||||
|  | <% end %> | ||||||
		Loading…
	
		Reference in New Issue