Skip to content

Instantly share code, notes, and snippets.

@caryamiel
Last active January 8, 2016 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caryamiel/5226141651044ad9c24a to your computer and use it in GitHub Desktop.
Save caryamiel/5226141651044ad9c24a to your computer and use it in GitHub Desktop.
RUBY ON RAILS: how to filter index action to send json file with an user_id:value equal to localhost:user/:ID/projects NEED HELP
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
skip_before_filter :verify_authenticity_token
before_action :set_user, only: [:show, :edit,:create, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = Project.all
render json: @projects
end
# GET /projects/1
# GET /projects/1.json
def show
render json: @project
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
@project.user_id = @user.id
if @project.save
render json: {status: :success, project: @project}
else
render json: {status: :failed, project: @project}
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
if @project.update(project_params)
render json: {status: :success, project: @project}
else
render json: {status: :failed, project: @project}
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :descriptions, :startDate, :dueDate)
end
def set_user
@user = User.find(params[:user_id])
end
end
class User < ActiveRecord::Base
has_secure_password
validates :name,
presence: true
validates :phonenumber,
presence: true
validates :email,
presence: true,
uniqueness: true,
format: {
with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
}
def to_s
"#{name} #{phonenumber}"
end
has_many :projects, dependent: :destroy
has_many :tasks, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :friendships, dependent: :destroy
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
end
class Project < ActiveRecord::Base
has_many :task, dependent: :destroy
belongs_to :user
end
resources :users do
resources :friendships
resources :projects do
resources :tasks do
resources :subtasks do
resources :comments
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment