I am having a bad time trying to get some form parameters (via a simple
form, latest version), stored in an integer array in a PostgreSQL table.
I
have abstracted the key parts below, but if anyone would like to poke
around, I have hosted the project on GitHub:
https://github.com/cjbutcher/avc_risk_manager_2. Any help much
appreciated,
I have wrestled with this problem for days!
I am building a risk management app. A new risk should accept an integer
from the ‘impact’ and ‘likelihood’ fields on the form view and insert
them
into arrays in the risk table. At the moment, all fields on the new/edit
risk form update correctly, except impact and likelihood (the arrays).
If I
enter values in these fields, no error is thrown, yet they are not
updated.
Here is the code for the form. I don’t think the problem lies here, but
here it is anyway:
app/views/shared/_risk_form.html.erb
<%= simple_form_for(@risk) do |f| %>
<%= render ‘shared/error_messages’, object: f.object %>
Here is the controller, note the strong params for impact and
likelihood:
app/controllers/risk_controller.rb
class RisksController < ApplicationController
before_action :signed_in_user
before_action :correct_user, only: [:destroy, :update]
def create
@risk = current_user.risks.build(risk_params)
if @risk.save
flash[:success] = “Risk created!”
redirect_to root_url
else
render ‘new’
end
end
def new
@risk = Risk.new
end
def destroy
@risk.destroy
redirect_to root_url
end
def edit
@risk = Risk.find(params[:id])
end
def update
@risk = Risk.find(params[:id])
@risk.assign_attributes(risk_params)
if @risk.changed? == false
flash[:info] = “No changes were made”
redirect_to root_url
elsif @risk.update_attributes(risk_params)
flash[:success] = “The risk has been updated.”
redirect_to root_url
else
render ‘new’
end
end
private
def risk_params
params.require(:risk).permit(:description, :title, :area, :owner,
:action,
:date_of_action, :action_completed, { :impact => [] }, { :likelihood =>
[]
})
end
def correct_user
if current_user.admin?
@risk = Risk.find_by(id: params[:id])
redirect_to root_url if @risk.nil?
else
@risk = current_user.risks.find_by(id: params[:id])
redirect_to root_url if @risk.nil?
end
end
end
Here is the model. I purposefully left validations off the impact and
likelihood fields for now so I don’t have to worry about that being the
problem:
app/models/risk.rb
class Risk < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :description, presence: true
validates :title, presence: true
validates :area, presence: true
validates :owner, presence: true
end
Finally, here is the db schema:
app/db/schema.rb
ActiveRecord::Schema.define(version: 20140622153923) do
These are extensions that must be enabled in order to support this
database
enable_extension “plpgsql”
create_table “risks”, force: true do |t|
t.integer “user_id”
t.string “description”
t.datetime “created_at”
t.datetime “updated_at”
t.string “title”
t.string “area”
t.string “owner”
t.string “action”
t.date “date_of_action”
t.boolean “action_completed”, default: false
t.integer “impact”, default: [], array: true
t.integer “likelihood”, default: [], array: true
end
add_index “risks”, [“user_id”, “created_at”], name:
“index_risks_on_user_id_and_created_at”, using: :btree
create_table “users”, force: true do |t|
t.string “name”
t.string “email”
t.boolean “admin”, default: false
t.datetime “created_at”
t.datetime “updated_at”
t.string “password_digest”
t.string “remember_token”
end
add_index “users”, [“email”], name: “index_users_on_email”, unique:
true,
using: :btree
add_index “users”, [“remember_token”], name:
“index_users_on_remember_token”, using: :btree
end
Any help much appreciated! If there is any extra information I can
provide
please let me know.
Thanks v much, Chris