From 27ebee595014926419b6ccba922ad39450b73f6b Mon Sep 17 00:00:00 2001 From: Yang Zhao Date: Sun, 1 Jan 2017 11:23:26 +0000 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=9B=E5=BB=BA=E5=92=8C?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 50 ++++++++++++-------------- app/models/project.rb | 2 +- db/seeds.rb | 3 -- public/index.html | 4 +++ public/pages/include/main.js | 3 -- public/pages/project_select.html | 11 ++++-- 6 files changed, 36 insertions(+), 37 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 6b21eb1..dde959a 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -4,20 +4,20 @@ class ProjectsController < ApplicationController skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail] #获取我的项目列表,user_id保存在session中. - def index #Todo uid = session[:user_id] - pid = Projects_user.find_by(project_id: uid) - mc = Mission.where(:project_id => pid).size - uc = Projects_user.where(:project_id => pid).size - sc = Share.where(:project_id => pid).size - data = [] - Project.find(pid) do |i| - data += [{ - :id => i.id, - :name => i.name, - :content => i.content, + data=[] + Projects_user.where(:user_id => uid).each do |i| + pid=i.project_id + p=Project.find_by(id:pid) + mc = Mission.where(:project_id => pid).size + uc = Projects_user.where(:project_id => pid).size + sc = Share.where(:project_id => pid).size + data+=[{ + :id => pid, + :name => p.name, + :content => p.content, :mission_count => mc, :users_count => uc, :shares_count => sc @@ -30,22 +30,15 @@ class ProjectsController < ApplicationController end #创建项目 - def create - #Todo - project = Project.new() - project.content = params[:content] - project.name = params[:name] - now = Time.new - project.created_at = now - project.updated_at = now - project.save! - # 将自己加入项目 - pu = Project_user.new() - pu.project_id = project.id - pu.user_id = session[:user_id].to_i - pu.save! - render :json => {:code => 0} + uid = session[:user_id] + project = Project.new(project_params) + if project.save + Projects_user.create(project_id:project.id,user_id:uid) + render status: :created, nothing: true + else + render json: project.errors, status: :unprocessable_entity + end end #项目添加用户 @@ -94,5 +87,8 @@ class ProjectsController < ApplicationController } } end - + private + def project_params + params.require(:project).permit(:name,:content) + end end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index e54aece..24e0dc1 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,7 +1,7 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :users - validate :require_at_least_on_user,on:addUsers # 项目中最少要有一个用户 + validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户 validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true private diff --git a/db/seeds.rb b/db/seeds.rb index 0625414..c849e66 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,9 +9,6 @@ users = [ {:name => '杨诏', :password => '123123123', :email => 'lucio.yang@qq.com', :phone => '15652591529', :created_at => "2016/12/27", :updated_at => "2016/12/27"}, {:name => '陈翊', :password => '123456', :email => '1085730215@qq.com', :phone => '18269771988', :created_at => "2016/12/28", :updated_at => "2016/12/27"} ] -projects = [ - {:id=>'1001',:name => 'AAAA',:content => 'i like helloworld',:mission_count => '20',:users_count => '15',:shares_count => '15'} -] users.each do |user| User.create!(user) diff --git a/public/index.html b/public/index.html index 2f3aeaa..070f9bd 100644 --- a/public/index.html +++ b/public/index.html @@ -174,6 +174,7 @@ type: "get", dataType: "json", url:"/session.json", + async: false, statusCode: { 200: function(data) {// 请求成功 $("#name1").text(data.name); @@ -192,6 +193,9 @@ } }); }); + if(getProjectId()==null){ + window.location="pages/project_select.html"; + } function logout(){ $.ajax({ type: "delete", diff --git a/public/pages/include/main.js b/public/pages/include/main.js index 0666944..4c0ae11 100644 --- a/public/pages/include/main.js +++ b/public/pages/include/main.js @@ -1,7 +1,4 @@ function initCurrent(){ - if(getProjectId()==null){ - window.location="pages/project_select.html"; - } LoginUserInfo={ selectProject:getProjectId(), name:'', diff --git a/public/pages/project_select.html b/public/pages/project_select.html index 56c410a..24fd144 100644 --- a/public/pages/project_select.html +++ b/public/pages/project_select.html @@ -230,22 +230,27 @@ }); }); $("#creat_project").click(function(){ + var pName=$("#exampleName").val(); + var pContent=$("#exampleTextarea").val(); $.ajax({ type:"post", dataType:"json", url:"/projects.json", data:{ - "name":$("#exampleName").val(), - "content":$("#exampleTextarea").val() + "project[name]":pName, + "project[content]":pContent }, statusCode: { - 200: function() {// 请求成功 + 201: function() {// 请求成功 alert("创建成功!"); window.location.reload(); }, 401:function(){// 未授权 alert("未登录!"); window.location.href="login.html"; + }, + 422:function(){ + alert("项目名重复!"); } } });