Quick overview of what I want my app to do
I have a Job Sheet app where users can submit job sheets after the work they have done.
There are 2 types of users there is normal users and admin users.
The admin Users will submit a form with the customers info on and when the person doing the job has finished the job he will log in and fill in his details in for the job.
The Problem
I want to have a select box that only admin users can see when submitting a job sheet that will containt a list of the users and the value’s will be their user id.
If the user is not an admin he will not see this.
I have the select box working and the paramaters are pointing to the correct user_id when posting. However beasue I am setting the user_id in the controller it overides the user_id from the form, how could i re-write this code to fix the problem I am having?
The Code
class JobSheetsController < ApplicationController
before_filter :authorize
def index
@company = find_company
@job_sheets = @company.job_sheets.for_user(current_user)
end
def new
@company = find_company
@job_sheet = @company.job_sheets.new(user: current_user)
@job_sheet.products.new
@users = User.all
end
def create
@company = find_company
@job_sheet = @company.job_sheets.new(job_sheet_params)
@job_sheet.user = current_user
@job_sheet.save
redirect_to company_job_sheets_path(@company)
end
def show
@company = find_company
@job_sheet = JobSheet.for_user(current_user).find(params[:id])
end
def edit
@company = find_company
@job_sheet = find_job_sheet(@company)
end
def update
@company = find_company
@job_sheet = find_job_sheet(@company)
@job_sheet.update_attributes(job_sheet_params)
redirect_to company_job_sheets_path(@company)
end
def destroy
@company = find_company
@job_sheet = find_job_sheet(@company)
@job_sheet.destroy
respond_to do |format|
format.html { redirect_to company_job_sheets_path(@company) }
format.js
end
end
private
def find_company
Company.find(params[:company_id])
end
def find_job_sheet(company)
company.job_sheets.for_user(current_user).find(params[:id])
end
def job_sheet_params
params.require(:job_sheet).permit(:user_id, :address, :postcode, :date, :po_number,:contact,
:phone, :job_type, :reg_no, :vehicle, :vin_no,
:vehicle_milage, :job_instructions, :check_pre,
:check_info, :check_reported_to, :check_customer_aknowledgment,
:post, :info, :reported_to, :customer_aknowledgment,
:certify, :confirm, :name, :comment, :zibox_no, :sim_no,
:safe_box, :new_zibox_no, :new_sim_no, :returned,
:service_call_info, :mobile, products_attributes: [:id, :product_code, :product_description, :alloc_qty, :used, :returned, :_destroy] )
end
end
class JobSheet < ActiveRecord::Base
belongs_to :company
belongs_to :user
has_many :products, dependent: :destroy
accepts_nested_attributes_for :products, reject_if: :reject_products, allow_destroy: true
def self.for_user(user)
if user.aas_admin?
order("id DESC")
else
where(user: user)
end
end
def reject_products(attributed)
attributed['product_code'].blank?
end
end
<%= form_for([@company, @job_sheet]) do |form| %>
<%= form.select :user_id, @users.map{|user| [user.full_name, user.id]} if current_user.aas_admin? %>
<div>
<%= form.label :address, "Installation Address" %><br>
<%= form.text_area :address %><br>
<%= form.label :postcode %><br>
<%= form.text_field :postcode %>
</div>
...
Any help is appreciated, if you need more info please ask