JITENDRA KUSHVAHA
JITENDRA KUSHVAHA

Reputation: 147

How to insert selected value from f.select in rails

I want to insert selected value from select tag into data base when i submit form.

The problems is all value select from select tag, But when i submit form then only first value is always inserting into data base, not selected value.

My _form.html.erb file

<div class="form-group">
  <label class="control-label">Requirement Type</label>
  <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-globe"></i> </span>
     <%= f.select :job_types, Settings.job_types_for_job_posting.to_a.map { |w| [w.humanize, w] }, {}, class: "form-control" %>
   </div>
</div>

My Enum Define in setting.yml file

job_types_for_job_posting: [Contract, Permanent, On Demand, Internal]

My jobs_controller.rb file is

    def create
    @job = current_user.job_postings.build(params_value)

    respond_to do |format|
      if @job.save
        add_or_update_skills
        format.json { render json: { result: true } }
        format.html { redirect_to root_path, notice: 'Job is posted successfully' }
      else
        format.json { render json: { result: false } }
        format.html { render :new }
      end
    end
  end

    params.require(:job_posting).permit(:title, :description, :salary_from, :salary_to, :experience, :experienceto,
                                        :expected,:key,:sourcing,:client_id, :job_assignment,:status, :no_of_opening, :location, :designation, :responsibilities, :job_skills, :industry_id, :functional_area_id, :role_category_id, :job_role_id, :job_types)
  end 

When I am posing new job then every time insert only "Contract" means 0. Can any body tell me, Where am i wrong...

Upvotes: 1

Views: 469

Answers (1)

Fallenhero
Fallenhero

Reputation: 1583

(without having tested it) I think your line should read:

<%= f.select(:job_types, Settings.job_types_for_job_posting.to_a.collect.with_index { |w, i| [w.humanize, i] }, {}, class: "form-control") %>

The index of the array is supposed to be your value, right?

Upvotes: 1

Related Questions