invoice_keeper/app/controllers/notes_controller.rb

58 lines
1.3 KiB
Ruby

class NotesController < ApplicationController
def index
@client = Client.where(id: params[:client_id]).first
@notes = Note.where(client_id: params[:client_id]).order(name: :asc)
end
def show
@note = Note.where('client_id = ? AND id = ?', params[:client_id], params[:id])
end
def new
@note = Note.new
@client = Client.where(id: params[:client_id]).first
end
def create
@note = Note.new(note_params)
@note.client_id = params[:client_id]
@client = Client.where(id: params[:client_id]).first
if @note.save
redirect_to notes_path(client_id: @client.id)
else
render :new
end
end
def edit
@note = Note.find(params[:id])
@client = Client.where(id: params[:client_id]).first
end
def update
@note = Note.find(params[:id])
@client = Client.where(id: params[:client_id]).first
if @note.update(note_params)
redirect_to notes_path(client_id: @client.id)
else
render 'edit'
end
end
def destroy
@note = Note.find(params[:id])
@client = Client.where(id: params[:client_id]).first
@note.destroy unless @note.blank?
redirect_to notes_path(client_id: @client.id)
end
private
def note_params
params.require(:note).permit(:name, :content)
end
end