Started POST "/risks" for 127.0.0.1 at 2014-06-29 12:58:02 +0100
Processing by RisksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LOgpMvsEGkhQaoZX5BkrX+Nw1Ov9QQaHuQqWO+L8peI=", "risk"=>{"title"=>"aa", "description"=>"aadad", "area"=>"IT", "owner"=>"5", "action"=>"dadad", "date_of_action(3i)"=>"29", "date_of_action(2i)"=>"6", "date_of_action(1i)"=>"2014", "action_completed"=>"0"}, "impact"=>["4"], "likelihood"=>["4"], "commit"=>"Create Risk"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '1b3c0548b48b9f90cab54a695297436286ad0132' LIMIT 1
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO "risks" ("action", "area", "created_at", "date_of_action", "description", "owner", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["action", "dadad"], ["area", "IT"], ["created_at", "2014-06-29 11:58:02.816205"], ["date_of_action", "2014-06-29"], ["description", "aadad"], ["owner", "5"], ["title", "aa"], ["updated_at", "2014-06-29 11:58:02.816205"], ["user_id", 2]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 9ms (ActiveRecord: 1.2ms)
You can still see that the impact and likelihood fields are still omitted during the commit. Maybe this is because they are being passed in as strings (denoted by the quotes around the values you can see being passed in the terminal output above), even though the array is explicitly integer? I have tried forcing integers like this, but it has made no difference:
<span class="impact-input">
<%= f.input :impact, as: :integer, required: false, :error => false, input_html: { class: 'form-control', :name => "impact[]" } %>
</span>
<span class="likelihood-input">
<%= f.input :likelihood, as: :integer, required: false, :error => false, input_html: { class: 'form-control', :name => "likelihood[]" } %>
</span>
So I'm still stuck! If I change the input name like I have, would I have to adjust anything else in my application? This is my first rails project so forgive me if the answer to that is an obvious 'of course'.
Thank you both for your help thus far.
On Monday, June 23, 2014 10:12:04 PM UTC+1, Chris Butcher wrote:
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 %><div class="form-group"><%= f.input :title, required: false, :error => false, input_html: { class: 'form-control' } %></div><div class="form-group"><%= f.input :description, required: false, :error => false, as: :text, input_html: { class: 'form-control description' } %></div><div class="form-group"><%= f.input :area, :collection => ['Operations', 'IT', 'Finance'], required: false, :error => false, input_html: { class: 'form-control' } %></div><div class="form-group"><%= f.input :owner, :collection => User.all, required: false, :error => false, input_html: { class: 'form-control' } %></div><div class="form-group"><%= f.input :action, required: false, :error => false, input_html: { class: 'form-control' } %></div><div class="form-inline"><span class="date-of-action-input"><%= f.input :date_of_action, as: :date, :start_year => Date.today.year - 10, :end_year => 2030,:order => [ :day, :month, :year], :required => false, :error => false, input_html: { class: 'form-control' } %></span><span class="action-completed-input"> <%= f.input :action_completed, as: :boolean, required: false, :error => false, input_html: { class: 'form-control' } %></span><span class="impact-input"><%= f.input :impact, required: false, :error => false, input_html: { class: 'form-control' } %></span><span class="likelihood-input"><%= f.input :likelihood, required: false, :error => false, input_html: { class: 'form-control' } %></span><span class="submit-risk"><%= f.button :submit, :error => false, :error => false, input_html: { class: 'form-control' } %></span></div><% end %>Here is the controller, note the strong params for impact and likelihood:app/controllers/risk_controller.rb class RisksController < ApplicationControllerbefore_action :signed_in_userbefore_action :correct_user, only: [:destroy, :update]def create@risk = current_user.risks.build(risk_params) if @risk.saveflash[:success] = "Risk created!"redirect_to root_urlelserender 'new'endenddef new@risk = Risk.newenddef destroy@risk.destroyredirect_to root_urlenddef edit@risk = Risk.find(params[:id])enddef update@risk = Risk.find(params[:id])@risk.assign_attributes(risk_params) if @risk.changed? == falseflash[:info] = "No changes were made"redirect_to root_urlelsif @risk.update_attributes(risk_params) flash[:success] = "The risk has been updated."redirect_to root_urlelserender 'new'endendprivatedef risk_paramsparams.require(:risk).permit(:description, :title, :area, :owner, :action, :date_of_action, :action_completed, { :impact => [] }, { :likelihood => [] }) enddef correct_userif 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?endendendHere 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.rbclass Risk < ActiveRecord::Basebelongs_to :uservalidates :user_id, presence: truevalidates :description, presence: truevalidates :title, presence: truevalidates :area, presence: truevalidates :owner, presence: trueendFinally, here is the db schema:app/db/schema.rbActiveRecord::Schema.define(version: 20140622153923) do # These are extensions that must be enabled in order to support this databaseenable_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: falset.integer "impact", default: [], array: truet.integer "likelihood", default: [], array: trueendadd_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: falset.datetime "created_at"t.datetime "updated_at"t.string "password_digest"t.string "remember_token"endadd_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btreeadd_index "users", ["remember_token"], name: "index_users_on_remember_token", using: :btree endAny help much appreciated! If there is any extra information I can provide please let me know.Thanks v much, Chris
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f2e56569-a889-4dd1-ab17-695178865738%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment