1class SharesController < ApplicationController
  • remove trailing whitespace » rails_best_practices
2   
 
3    before_action :authenticate
 
4    skip_before_action :verify_authenticity_token, :only => [
 
5        :create,
 
6        :getShareList
 
7    ]
 
8    #
 
 9   
  • use model association (for share) » rails_best_practices
10    def create
 
11        uid = session[:user_id].to_i
 
12        share=Share.new(share_params)
 
13        share.user_id=uid
 
14        if share.save
 
15            content = "<a>"+User.find(uid).name + "</a> "
 
16            pid=share.project_id
 
 
18            Projects_user.where(:project_id => pid).each do |i|
 
19                params_note = {user_id: i.user_id,content: content,category: 1,project_id: pid}
 
20                note = Note.new(params_note)
 
21                note.save!
 
22            end
 
23            render status: :created, nothing: true
 
24        else
 
25            render json: project.errors, status: :unprocessable_entity
 
26        end
 
27    end
 
 
29    #
 
 
31    def getShareList
 
32    #Todo
 
33    pid = params[:project_id]
 
 
35    data = []
 
36    Share.where(:project_id => pid).order("created_at desc").each do |i|
 
37        puts i.content
 
38        name = User.find(i.user_id).name
 
39        data += [{
 
40            :name => name,
 
41            :time => i.created_at,
 
42            :content => i.content
 
43        }]
 
44    end
 
45    render :json => {
 
46        :code => 0,
 
47        :data => data
 
48    }
 
49    end
 
 
51    private
 
52        def share_params
 
53          params.require(:share).permit(:project_id,:content)
 
54        end
 
55end