Search code examples
ruby-on-railssimple-form

Simple_form does not send value after adding columns in the schema


I wanted to updated a simple_form with more values that the user can input.

In order to do so I:

1) Create a new migrations adding in my database questions the new columns. The new columns apper is schema.db, so I assumed the new migration worked.

2) Add in the simple_form, the "new" f.input linked to the new columns.

However, once I submit the simple_form, the values of the new columns are not passed to the database while the old ones are passed.

new.html.erb

<div class="container">

  <div class="row ">
    <div class="col-sm-6 col-sm-offset-3">

      <%= simple_form_for [@user, @project, @paper, @question], url: project_paper_questions_path do |f| %>
        <div class="form-inputs">

      <%= f.input :question_content_year, label: "1. Year"%>
      <%= f.input :question_content_country, label: "2. Country"%>
      <%= f.input :question_content_income, :collection =>[["Low income", -1], ["Middle income", 0], ["High income", 0.5]], label: "3. Income level" %>
      <%= f.input :question_content_type, :collection =>[["CUA", 0], ["CEA", 1], ["CBA", 2], ["CCA", 3], ["CMA", 4]], label: "4. Type of economic evaluation" %>
      <%= f.input :question_content_study, :collection =>[["RCT", 0], ["Quasi-experimental", 1], ["Modelling", 2], ["Observational", 3], ["Mixed RCT and Modelling", 4]], label: "5. Study Design " %>
      <%= f.input :question_content_modelling, :collection =>[["Decison Tree", 0], ["Markov model", 1], ["Patient level simulation/microsimulation", 2], ["Dynamic models", 3], ["Other", 4]], label: "6. Type of modeling, if modeling design " %>
      <%= f.input :question_content_perspective_a, :collection =>[["Social", 0], ["Provider/Health care system", 1], ["Program", 2], ["Patient", 3], ["Payer/Third party", 4], ["Mixed", 5]], label: "7. Perspective (as stated by authors)" %>
      <%= f.input :question_content_perspective_r, :collection =>[["Social", 0], ["Provider/Health care system", 1], ["Program", 2], ["Patient", 3], ["Payer/Third party", 4], ["Mixed", 5]], label: "8. Perspective (evaluated by reviewer)" %>
      <%= f.input :question_content_sensitivity, :collection =>[["One way/Univariate", 0], ["Multy-way/Multivariate", 1], ["Probabilistic analysis", 2], ["Not performaed/specified", 3], ["CMA", 4]], label: "9. Type of sensitivity analysis" %>
      <%= f.input :question_content_type, :collection =>[["<= 1", 0], ["1-10 years", 1], ["Over 10 years", 2], ["Not clearly stated", 3]], label: "10. Time horizon" %>
      <%= f.input :question_content_outcome, :collection =>[["QALY/DALY", 0], ["Natural units", 1], ["Process outcomes", 2], ["Monetary", 3]], label: "11. Outcome Measure" %>
      <%= f.input :question_content_intervention, label: "12. Decription of Intervention/Comparison" %>
      <%= f.input :question_content_data, :collection =>[["Primary", 0], ["Secondary", 1], ["Mixed", 2], ["Monetary", 3]], label: "13. Type and source of data used" %>
      <%= f.input :question_content_sample, label: "14. Sample Size" %>
      <%= f.input :question_content_description, label: "15. Description of analysis outcomes" %>



      **THE FOLLOWING ARE THE OLD QUESTIONS/COLUMNS AND THEY WORK


      <%= f.input :question_1, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "1. Identify the study as an economic evaluation" %>
      <%= f.input :question_2, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "2. Provide a structured summary of objectives, methods, results and conclusions" %>
      ...
      " %>
      <%= f.input :question_28, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "24. Describe any potential for conflict of interest among study contributors in accordance with journal policy.
      " %>


      <%= f.association :user, label: "Which user is creating it?", :as => :hidden, :input_html => { :value => current_user.id }  %>
      </div>


      <div class="form-actions">
        <%= f.button :submit, "Send your review" %>
      </div>
    </div>
    <% end %>
  </div>

schema.db

  create_table "questions", force: :cascade do |t|
    t.bigint "user_id"
    t.bigint "paper_id"
    t.bigint "project_id"
    t.string "type"
    t.string "question_1"
    t.string "question_2"
    t.string "question_3"
    t.string "question_4"
...
    t.string "question_28"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false

THE FOLLOWING ARE THE COLUMNS I ADDED AND DO NOT WORK

    t.string "question_content_year"
    t.string "question_content_income"
    t.string "question_content_condensed_type"
    t.string "question_content_study"
    t.string "question_content_modelling"
    t.string "question_content_perspective_a"
    t.string "question_content_perspective_r"
    t.string "question_content_sensitivity"
    t.string "question_content_type"
    t.string "question_content_outcome"
    t.string "question_content_intervention"
    t.string "question_content_data"
    t.string "question_content_sample"
    t.string "question_content_description"
    t.string "question_content_country"
    t.index ["paper_id"], name: "index_questions_on_paper_id"
    t.index ["project_id"], name: "index_questions_on_project_id"
    t.index ["user_id"], name: "index_questions_on_user_id"
  end

Controller

  def new
    @project = Project.find(params[:project_id])
    @paper = Paper.find(params[:paper_id])
    @question = Question.new
  end

  def create
    @question = Question.new(question_params)
    @question.project = Project.find(params[:project_id])
    @question.paper = Paper.find(params[:paper_id])
    if @question.save
      redirect_to projects_path
    else
      flash[:alert] = "Some error !"
      render :new
    end
  end

Only the old values (from question_1 to question_28 are sent to the database, while the new one - example: :question_content_year are not passed). If if I check with rails console --> Question.last --> The new columns have value of nil.

Nil value for the columns I added The values of the new columns are not sent


Solution

  • I think the problem is that you have not modified the question_params method in your controller. You need to list the new fields in the permit call there.

    The purpose of this permit mechanism is to whitelist attributes that you expect to be submitted with the form so that a maliciously crafted form submit can't overwrite fields that are not supposed to be updated from that form.