Simple_form 在架构中添加列后不发送值
Simple_form does not send value after adding columns in the schema
我想用更多用户可以输入的值来更新 simple_form
。
为了做到这一点我:
1) 创建一个新的迁移,在我的数据库中添加 questions
新列。新列应用程序是 schema.db
,因此我假设新迁移有效。
2) 添加在simple_form
, "new" f.input
链接到新的列。
但是,一旦我提交 simple_form
,新列的值不会传递到数据库,而旧列的值会被传递。
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
控制器
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
只有旧值(从 question_1
到 question_28
被发送到数据库,而新值 - 例如::question_content_year
不会被传递)。
如果我检查 rails console
--> Question.last
--> 新列的值为 nil
.
我认为问题在于您没有修改控制器中的 question_params
方法。您需要在 permit
调用中列出新字段。
此许可机制的目的是将您希望与表单一起提交的属性列入白名单,以便恶意制作的表单提交无法覆盖不应从该表单更新的字段。
我想用更多用户可以输入的值来更新 simple_form
。
为了做到这一点我:
1) 创建一个新的迁移,在我的数据库中添加 questions
新列。新列应用程序是 schema.db
,因此我假设新迁移有效。
2) 添加在simple_form
, "new" f.input
链接到新的列。
但是,一旦我提交 simple_form
,新列的值不会传递到数据库,而旧列的值会被传递。
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
控制器
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
只有旧值(从 question_1
到 question_28
被发送到数据库,而新值 - 例如::question_content_year
不会被传递)。
如果我检查 rails console
--> Question.last
--> 新列的值为 nil
.
我认为问题在于您没有修改控制器中的 question_params
方法。您需要在 permit
调用中列出新字段。
此许可机制的目的是将您希望与表单一起提交的属性列入白名单,以便恶意制作的表单提交无法覆盖不应从该表单更新的字段。