1

I am creating a website where a user (interviewer) can create a position (a job opening).

Even if the params are sent, my create action does not save them except for the current_user.

This is what I send: My params when I select Javascript and ruby from the checkbox

positions_controller.rb

  def new
    @position = Position.new
  end

  def create
    @position = Position.new(position_params)
    @position.interviewer = current_interviewer
    if @position.save
      redirect_to position_path(@position)
      flash[:success] = "You created a new position/opening"

    else
      render :new
    end
    raise
  end
  private

  def set_position
    @position = Position.find(params[:id])
  end

  def position_params
    params.require(:position).permit(:title, :skills, :interviewer)
  end

end

_form.html.erb

 <%= simple_form_for [@interviewer, @position] do |f| %>

    <%= f.input :title, required:true %>

    <%= f.input :skills, as: :check_boxes, collection:[
    ['Python', "python"],
    ['Java', "java"],
    ['JavaScript', "javascript"],
    ['Ruby', "ruby"],
    ['C++', "c++"],
    ['Node.js', "node"],
    ['React', "react"],
    ['Django', "django"],
    ['Rails', "rails"],
    ['SQL', "sql"],
    ['Doker', "doker"],
    ['AWS', "aws"],
    ['Vue.js', "vue"],
    ['Marketing', "Marketing"],
    ['HR', "hr"],
    ['Finance', "finance"],
    ['IT', "it"],
    ], input_html: { multiple: true } %>

    <%= f.submit "Submit position", class: "btn btn-primary" %>
  <% end %>

position.rb

class Position < ApplicationRecord
  validates :title, presence: true

  belongs_to :interviewer
end

schema

  create_table "positions", force: :cascade do |t|
    t.string "title"
    t.bigint "interviewer_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "candidate_id"
    t.string "candidatures", default: [], array: true
    t.string "skills"
    t.index ["candidate_id"], name: "index_positions_on_candidate_id"
    t.index ["interviewer_id"], name: "index_positions_on_interviewer_id"
  end

My alternative was to replace the create code with:

@position = current_interviewer.positions.new(position_params)  

but it still does not work.

0

2 Answers 2

1

Since you have a input_html: { multiple: true } for the params skills, you need to add the following in positions_controller.rb:

 def position_params
    params.require(:position).permit(:title, :interviewer, :candidate, skills:[])
  end

Basically, your skills will be saved as an array if you allow input_html: { multiple: true } for a collection

In addition, you are not passing any params for candidate

Sign up to request clarification or add additional context in comments.

Comments

1

In your table, you have interviewer_id but in your permitted params you have interviewer.

Change your params to permit interviewer_id instead.

Also, in your form you have <%= f.input :title, required:true %> Presence is required by default (at least with simple_form). You don't need to declare it in the form, but you should still keep it in your Model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.