how to implement jobpost functionality that has 3 fixed categoris
Asked Answered
C

2

0

What i want to do is create placement model which will be used to post jobs for 3 fixed categories i.e internship,corporate and freelance.There will be a menu of placement which will have 3 menues as internship,corporate and freelance and based on user selection ,for these 3 categories same job post form will be shown for creating job posts. While showing jobposts diffrent view will be shown based on category.

I am not getting how i should implement this.Whether i should create placement and categories as a different model and give association as has-many categories in placement and belongsto placement in category, but if i do that way,in categories model i have 3 fixed choices and i am not going to accept that choice from user,so how can i add these 3 choices in the model which will be fixed and add jobposts for them categorywise?

How can i implement this placement thing?

Coincident answered 10/5, 2012 at 12:43 Comment(0)
C
0

It's simple you can achieve in various ways one way is create job_type which has one of 3 categories and give dropdown while creating job post.

For easy to find out create scope for 3 categories like

scope :freelance,where("job_type = ?",'freelance') ....

JobPost.freelance gives freelance job post.

Currish answered 10/5, 2012 at 13:54 Comment(0)
S
0

Define the models as

Class Jobpost
  belongs_to :resource, :polymorphic => true, :dependent => :destroy
  accepts_nested_attributes_for :resource
  def resource_attributes=(params = {})
    self.resource = spec_type.constantize.new unless self.resource
    self.resource.attributes = params.select{|k| self.resource.attribute_names.include?(k) || self.resource.class::ACCESSOR.include?(k.to_sym)}
   end
end

class Freelancer
  has_one :jobpost, :as => :resource
end

Add fields to Jobpost as to create fields(resource_id: integer, resource_type: string)

#jobposts table
t.references :resource, :polymorphic => true 

The view as

= form_for(@jobpost, :url => jobposts_path, :method => :post) do |f|
   = fields of Jobpost
   = f.fields_for :resource, build_resource('freelancer') do |freelancer|
     = fields of Freelancer

The Jobpost helper as

module JobpostsHelper
  def build_resource(klass)
    klass  = "{klass.capitalize}"
    object = eval("#{klass}.new")
    if @jobpost.resource.class.name == klass
      object = @jobpost.resource
    end
    return object
  end
end

Use javascript to show the fields of subcategory(freelancer, etc) when the link for Freelancer is clicked. when the form is submitted, all the fields for all the subcategories are submitted but they are filtered out in the 'resource_attributes=' method.

Shaun answered 28/8, 2012 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.