We are upgrading our application from Rails 4.2 to Rails 5.0.
We have found that identical requests are generating different sql when inserting in to the database. The database column is JSON and it looks like in Rails 5.0 it is being stored as an escaped string.
Does anyone know how we can ensure the same behaviour between versions?
Request Params:
Parameters: {"provider_package_item"=>{"provider_package_id"=>"2", "service_id"=>"6", "service_product_id"=>"17", "provider_price_list_id"=>"12", "provider_price_list_item_id"=>"122", "options"=>"[{\"id\":95,\"title\":\"Finish\",\"value\":{\"id\":\"130\",\"title\":\"Satin\",\"price\":0}},{\"id\":96,\"title\":\"Paper Weight\",\"value\":{\"id\":\"135\",\"title\":\"200GSM\",\"price\":0}},{\"id\":112,\"title\":\"Delivery\",\"value\":{\"id\":\"189\",\"title\":\"Next Day\",\"price\":0}}]"}}
Database Query in Rails 4.2:
INSERT INTO "provider_package_items" ("provider_package_id", "service_id", "service_product_id", "provider_price_list_id", "provider_price_list_item_id", "options", "title", "quantity", "price", "options_string", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["provider_package_id", 2], ["service_id", 6], ["service_product_id", 17], ["provider_price_list_id", 12], ["provider_price_list_item_id", 122], ["options", "[{\"id\":95,\"title\":\"Finish\",\"value\":{\"id\":\"130\",\"title\":\"Satin\",\"price\":0}},{\"id\":96,\"title\":\"Paper Weight\",\"value\":{\"id\":\"135\",\"title\":\"200GSM\",\"price\":0}},{\"id\":112,\"title\":\"Delivery\",\"value\":{\"id\":\"189\",\"title\":\"Next Day\",\"price\":0}}]"], ["title", "A4 Brochures"], ["quantity", 50], ["price", 65.0], ["options_string", "/Satin/200GSM/Next Day"], ["created_at", "2017-03-03 05:36:51.613297"], ["updated_at", "2017-03-03 05:36:51.613297"]]
Database Query in Rails 5.0
INSERT INTO "provider_package_items" ("title", "service_id", "service_product_id", "provider_price_list_id", "provider_price_list_item_id", "quantity", "options", "options_string", "created_at", "updated_at", "provider_package_id", "price") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["title", "A4 Brochures"], ["service_id", 6], ["service_product_id", 17], ["provider_price_list_id", 12], ["provider_price_list_item_id", 122], ["quantity", 50], ["options", "\"[{\\"id\\":95,\\"title\\":\\"Finish\\",\\"value\\":{\\"id\\":\\"130\\",\\"title\\":\\"Satin\\",\\"price\\":0}},{\\"id\\":96,\\"title\\":\\"Paper Weight\\",\\"value\\":{\\"id\\":\\"135\\",\\"title\\":\\"200GSM\\",\\"price\\":0}},{\\"id\\":112,\\"title\\":\\"Delivery\\",\\"value\\":{\\"id\\":\\"189\\",\\"title\\":\\"Next Day\\",\\"price\\":0}}]\""], ["options_string", "/Satin/200GSM/Next Day"], ["created_at", 2017-03-03 05:30:02 UTC], ["updated_at", 2017-03-03 05:30:02 UTC], ["provider_package_id", 2], ["price", 65.0]]
Rails 5 has been updated to comply with RFC 7159. JSON strings e.g. JSON.stringify() from Javascript are now saved as strings rather than a Hash/Array. To maintain the behaviour from previous versions of Rails you need to parse the JSON string before assigning:
Model.payload = JSON.parse("{\"key\":\"value\"}")
Or you can override the setter method in the model:
def payload=(value)
self[:payload] = value.is_a?(String) ? JSON.parse(value) : value
end
In https://github.com/rails/rails/issues/28292
Observations from kengreeff
In Console (Rails 4.2.2)
item = OrderItem.new(options: "{\"key\":\"value\"}")
=> #<OrderItem id: nil, options: {"key"=>"value"}>
item.options
=> {"key"=>"value"}
item.options.is_a?(String)
=> false
item.options.is_a?(Hash)
=> true
In Console (Rails 5.0.2)
item = OrderItem.new(options: "{\"key\":\"value\"}")
=> #<OrderItem id: nil, options: "{\"key\":\"value\"}">
item.options
=> "{\"key\":\"value\"}"
item.options.is_a?(String)
=> true
item.options.is_a?(Hash)
=> false
kamipo have mentioned this
rails 5
This behavior is because efaa6e4 to fix the issue #24234.
Please use
OrderItem.new(options: JSON.parse("{\"key\":\"value\"}")).
Related
I'm trying to attach a js file to a model and store in s3 but every time I run the attach method with file io objects, the server gets hung up in a recursive create and delete of the activestorage attachment database as you can see in these server logs:
ActiveStorage::Blob Create (0.5ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "p7bq76rurim77iao2r9a01pj0pwg"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.911872"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.5ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "e96f2305-a7dd-4e81-b4be-95e8c8d00df5"], ["created_at", "2022-08-30 21:06:54.917626"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (0.5ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "b705726b-b97d-47d6-bb70-de0c9d4dfa6b"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.3ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "zybcvyig2o0u7dgogisklfnig7xv"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.944469"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.3ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "23e176a4-3816-47fe-97a5-e2bb3af97a1d"], ["created_at", "2022-08-30 21:06:54.947629"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (0.3ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "7d0e1765-4d6a-46ed-8b29-012adecda684"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.4ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "8997y96c81r8fxcofbe854ivu2r1"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.965508"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.4ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "4646e337-2ae3-4afc-aab0-48f84af4657d"], ["created_at", "2022-08-30 21:06:54.969796"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (2.2ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "c687a1fa-7ba3-4e76-af20-a3867c153b95"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.5ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "83hcoeo5mdwo4glg3gjmb7jx2l34"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:54.998981"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Create (0.5ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "js_file"], ["record_type", "Property"], ["record_id", "c520252d-a889-4636-ad9c-30973429e25c"], ["blob_id", "9e053cf2-a248-44b9-bf37-b3005efea1be"], ["created_at", "2022-08-30 21:06:55.004691"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Attachment Destroy (0.3ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 [["id", "5bc44cce-62cf-4dc5-829e-97dfd9b28411"]]
↳ app/models/property.rb:22:in `make_js_file'
ActiveStorage::Blob Create (0.4ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["key", "a4c22dupjyndviedp41r6e8cg7dx"], ["filename", "c520252d-a889-4636-ad9c-30973429e25c.js"], ["content_type", "application/javascript"], ["metadata", "{\"identified\":true}"], ["service_name", "amazon"], ["byte_size", 13], ["checksum", "XY+qICmXk3H4sx9RJIwiMA=="], ["created_at", "2022-08-30 21:06:55.029049"]]
↳ app/models/property.rb:22:in `make_js_file'
I thought maybe where I run the attach method might affect it so I tried adding it in the model, controller, and tested in the console and I get the same result.
I also tried using File, Temfile, and StringIO but get the same result.
Here is an example
dir = Rails.root.join("tmp/files")
file = File.open("#{dir}/#{self.id}.js", "w") { |f| f.write "function(){};" }
self.js_file.attach(
io: File.open("#{dir}/#{self.id}.js"),
filename: "#{self.id}.js",
content_type: 'application/javascript'
)
Not sure what the problem might be.
I made a mistake. It seems like adding it in the model with a callback created a recursive loop when the attach method is applied to the model. Better to leave out of the model
I am trying to pass in an array through AngularJS ngResource 'POST' but having trouble with it. ngResource doesn't seem to like arrays being passed in through params? Is there any way to get round this so the correct associations will be made?
Controller
ctrl.recipe = new RecipeService();
ctrl.ingredients = IngredientService.query();
ctrl.recipe.carbs = 0;
ctrl.recipe.fat = 0;
ctrl.recipe.protein = 0;
ctrl.recipe.ingredient_ids = [];
ctrl.addRecipe = function() {
if (Auth.isAuthenticated()) {
ctrl.recipe.$save(function() {
var action = "created"
alerts.recipeSuccessAlert(action);
$location.path('recipes');
});
} else {
alerts.loginAlert();
};
};
ctrl.addIngredient = function(ingredient) {
ctrl.recipe.ingredient_ids.push(ingredient.id.toString());
console.log(ctrl.recipe.ingredient_ids)
};
Strong Params Rails
def recipe_params
params.require(:recipe).permit(:title, :serves, :method, :time, :carbs, :fat, :protein, :meal_type, :user_id, :calories, ingredient_ids: [])
end
Output
Started POST "/api/v1/recipes.json" for ::1 at 2016-04-29 12:19:52 +1000
Processing by Api::V1::RecipesController#create as JSON
Parameters: {"carbs"=>0, "fat"=>0, "protein"=>2, "ingredient_ids"=>["3", "2"], "title"=>"jklsad", "serves"=>4, "meal_type"=>"Breakfast", "method"=>"jkasljasdk", "time"=>"5", "calories"=>8, "recipe"=>{"title"=>"jklsad", "serves"=>4, "method"=>"jkasljasdk", "time"=>"5", "carbs"=>0, "fat"=>0, "protein"=>2, "meal_type"=>"Breakfast", "calories"=>8}}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.4ms) BEGIN
SQL (0.4ms) INSERT INTO "recipes" ("title", "serves", "method", "time", "carbs", "fat", "protein", "meal_type", "calories", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["title", "jklsad"], ["serves", 4], ["method", "jkasljasdk"], ["time", 5], ["carbs", 0.0], ["fat", 0.0], ["protein", 2.0], ["meal_type", "Breakfast"], ["calories", 8.0], ["user_id", 1], ["created_at", "2016-04-29 02:19:52.029494"], ["updated_at", "2016-04-29 02:19:52.029494"]]
(7.6ms) COMMIT
Completed 200 OK in 17ms (Views: 0.6ms | ActiveRecord: 8.7ms)
I've recently started experimenting with setting up a new local PostgreSQL server, hooked up to an existing Rails app. I have a table I am trying to insert to:
postgres=# \d+ events.t_sales_events
Table "events.t_sales_events"
Column | Type | Modifiers | Storage | Stats target | Description
------------+-----------------------------+---------------------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('events.t_sales_events_id_seq'::regclass) | plain | |
event_name | character varying(45) | not null | extended | |
actor_id | integer | | plain | |
actor_type | character varying(45) | | extended | |
json | jsonb | | extended | |
created | timestamp without time zone | not null | plain | |
modified | timestamp without time zone | not null | plain | |
deleted | timestamp without time zone | | plain | |
timestamp | timestamp without time zone | not null | plain | |
context | text | | extended | |
Indexes:
"t_sales_events_pkey" PRIMARY KEY, btree (id)
This is an example of an event that I'm trying to insert into t_sales_events:
event =
{
:event_name => "lead_created",
:context => nil,
:actor_type => "sales_user",
:timestamp => "2013-03-18T07:13:42.000+0000",
:json => {
:dc_id => "00AA000000AAaaAAaa1",
:name => "John Doe",
:title => "CEO",
:role => nil,
:phone => nil,
:company => "Does Does",
:email => "jdoe#doesdoes.com",
:dc_source => nil,
:lead_id => nil
},
:actor_id => nil,
:created => 2015-09-15 18:32:25 -0700,
:modified => 2015-09-15 18:32:33 -0700
}
And this is the error I get when I run SalesEvent.create(event):(0.2ms)
BEGIN
SQL (4.0ms) INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["actor_id", nil], ["actor_type", "sales_user"], ["context", nil], ["created", Wed, 16 Sep 2015 01:32:25 UTC +00:00], ["deleted", nil], ["event_name", "lead_created"], ["json", "---\n:sfdc_id: 00AA000000AAaaAAaa1\n:name: John Doe\n:title: CEO\n:role: \n:phone: \n:company: Does Does\n:email: jdoe#doesdoes.com\n:lead_id: \n"], ["modified", Wed, 16 Sep 2015 01:32:33 UTC +00:00], ["timestamp", Mon, 18 Mar 2013 07:13:42 UTC +00:00]]
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type json
DETAIL: Token "-" is invalid.
CONTEXT: JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
(0.1ms) ROLLBACK
*** ActiveRecord::StatementInvalid Exception: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type json
DETAIL: Token "-" is invalid.
CONTEXT: JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
It seems to me that the error arises because "---" is added to the beginning of the field labelled 'json', but I'm at a loss as to why.
EDIT: I've added the model for SalesEvent below. I did indeed have serialize :json in there. Apologies - I forgot that I'd added that in a somewhat blind attempt to try and resolve the same error.
class SalesEvent < ActiveRecord::Base
establish_connection :warehouse_development
self.table_name = "events.t_sales_events"
serialize :json
def self.get_all
SalesEvent.all
end
end
Removing that particular line returns the same 'Token "-" is invalid' error, albeit due to different input:
(0.1ms) BEGIN
SQL (4.2ms) INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["actor_id", nil], ["actor_type", "sales_user"], ["context", nil], ["created", Wed, 16 Sep 2015 06:09:00 UTC +00:00], ["deleted", nil], ["event_name", "lead_created"], ["json", {:sfdc_id=>"00AA000000AAaaAAaa1", :name=>"John Doe", :title=>"CEO", :role=>nil, :phone=>nil, :company=>"Does Does", :email=>"jdoe#doesdoes.com", :dc_source=>nil, :lead_id=>nil}], ["modified", Wed, 16 Sep 2015 06:09:07 UTC +00:00], ["timestamp", Mon, 18 Mar 2013 07:13:42 UTC +00:00]]
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type json
DETAIL: Token "-" is invalid.
CONTEXT: JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
(0.1ms) ROLLBACK
*** ActiveRecord::StatementInvalid Exception: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type json
DETAIL: Token "-" is invalid.
CONTEXT: JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
Turns out it was the incompatibility of the postgres connector in rails 3.2.17 with jsonb objects. After upgrading to 4.2.4, the error stopped cropping up.
I have used carrierwave in my application, but some reason I am getting this error --> Unpermitted parameters: avatar even thought I have permitted params.
I am using nested_form with Single Table Inheritance
here are the codes --> removed_url
and here are the server logs:
Started POST "/alerts" for 127.0.0.1 at 2014-05-05 15:44:51 -0500
Processing by AlertsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jRaArdof0tbBHCXmdPqNiNpainmqliIx0sS9HdaBEcw=", "alert"=>{"victims_attributes"=>{"0"=>{"first_name"=>"victim1", "last_name"=>"victim1", "race"=>"race", "gender"=>"gender", "hair_color"=>"black", "eye_color"=>"black", "height_ft"=>"12", "height_inch"=>"12", "age"=>"12", "age_unit"=>"month", "birth_date"=>"05/02/2014", "weight"=>"125", "additional_info"=>"125", "images_attributes"=>{"0"=>{"avatar"=>[#<ActionDispatch::Http::UploadedFile:0x00000007f23350 #tempfile=#<Tempfile:C:/Users/jk12907/AppData/Local/Temp/RackMultipart20140505-56936-1neutn>, #original_filename="IMG_4470.JPG", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"alert[victims_attributes][0][images_attributes][0][avatar][]\"; filename=\"IMG_4470.JPG\"\r\nContent-Type: image/jpeg\r\n">]}}, "_destroy"=>"false"}}, "created_at"=>"2014/05/07 15:43", "nic"=>"nic", "investigating_officer_name"=>"officername", "investigating_officer_title"=>"officername", "investigating_officer_phone"=>"officername", "agency_name"=>"agencyname", "agency_phone"=>"agencyphone", "reporting_agency_phone"=>"agencyphone", "reporting_agency_email"=>"agencyemail", "last_seen"=>"2014/05/06 15:43", "last_known_address"=>"address", "last_known_city"=>"city", "last_known_state"=>"state", "last_known_county"=>"travis", "last_known_zip"=>"78660", "site_of_incident"=>"site of incident", "suspects_attributes"=>{"0"=>{"first_name"=>"suspect", "last_name"=>"suspect", "race"=>"asian", "gender"=>"male", "hair_color"=>"black", "eye_color"=>"black", "height_ft"=>"12", "height_inch"=>"45", "age"=>"12", "age_unit"=>"months", "birth_date"=>"05/02/2014", "weight"=>"145", "additional_info"=>"add info", "_destroy"=>"false"}}, "vehicles_attributes"=>{"0"=>{"make"=>"ford", "model"=>"ford", "style"=>"make", "year"=>"1995", "color"=>"black", "license_plate_number"=>"TYX", "license_plate_state"=>"TX", "additional_info"=>"add info", "_destroy"=>"false"}}}, "commit"=>"Create Alert"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 ORDER BY "users"."id" ASC LIMIT 1
Unpermitted parameters: avatar
(0.0ms) BEGIN
SQL (0.0ms) INSERT INTO "alerts" ("agency_name", "agency_phone", "created_at", "investigating_officer_name", "investigating_officer_phone", "investigating_officer_title", "last_known_address", "last_known_city", "last_known_county", "last_known_state", "last_known_zip", "last_seen", "nic", "reporting_agency_email", "reporting_agency_phone", "site_of_incident", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING "id" [["agency_name", "agencyname"], ["agency_phone", "agencyphone"], ["created_at", "2014-05-07 15:43:00.000000"], ["investigating_officer_name", "officername"], ["investigating_officer_phone", "officername"], ["investigating_officer_title", "officername"], ["last_known_address", "address"], ["last_known_city", "city"], ["last_known_county", "travis"], ["last_known_state", "state"], ["last_known_zip", "78660"], ["last_seen", "2014-05-06 15:43:00.000000"], ["nic", "nic"], ["reporting_agency_email", "agencyemail"], ["reporting_agency_phone", "agencyphone"], ["site_of_incident", "site of incident"], ["updated_at", "2014-05-05 20:44:51.406138"]]
SQL (1.0ms) INSERT INTO "vehicles" ("additional_info", "alert_id", "color", "created_at", "license_plate_number", "license_plate_state", "make", "model", "style", "updated_at", "year") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id" [["additional_info", "add info"], ["alert_id", 23], ["color", "black"], ["created_at", "2014-05-05 20:44:51.409138"], ["license_plate_number", "TYX"], ["license_plate_state", "TX"], ["make", "ford"], ["model", "ford"], ["style", "make"], ["updated_at", "2014-05-05 20:44:51.409138"], ["year", 1995]]
SQL (0.0ms) INSERT INTO "people" ("additional_info", "age", "age_unit", "alert_id", "birth_date", "created_at", "eye_color", "first_name", "gender", "hair_color", "height_ft", "height_inch", "last_name", "race", "type", "updated_at", "weight") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING "id" [["additional_info", "125"], ["age", 12], ["age_unit", "month"], ["alert_id", 23], ["birth_date", "2014-02-05"], ["created_at", "2014-05-05 20:44:51.411138"], ["eye_color", "black"], ["first_name", "victim1"], ["gender", "gender"], ["hair_color", "black"], ["height_ft", 12], ["height_inch", 12], ["last_name", "victim1"], ["race", "race"], ["type", "Victim"], ["updated_at", "2014-05-05 20:44:51.411138"], ["weight", 125]]
SQL (0.0ms) INSERT INTO "images" ("created_at", "person_id", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2014-05-05 20:44:51.414138"], ["person_id", 42], ["updated_at", "2014-05-05 20:44:51.414138"]]
SQL (0.0ms) INSERT INTO "people" ("additional_info", "age", "age_unit", "alert_id", "birth_date", "created_at", "eye_color", "first_name", "gender", "hair_color", "height_ft", "height_inch", "last_name", "race", "type", "updated_at", "weight") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING "id" [["additional_info", "add info"], ["age", 12], ["age_unit", "months"], ["alert_id", 23], ["birth_date", "2014-02-05"], ["created_at", "2014-05-05 20:44:51.416138"], ["eye_color", "black"], ["first_name", "suspect"], ["gender", "male"], ["hair_color", "black"], ["height_ft", 12], ["height_inch", 45], ["last_name", "suspect"], ["race", "asian"], ["type", "Suspect"], ["updated_at", "2014-05-05 20:44:51.416138"], ["weight", 145]]
(19.0ms) COMMIT
Redirected to http://127.0.0.1:3000/alerts
Completed 302 Found in 97ms (ActiveRecord: 21.0ms)
I have permitted the params in alerts_controller, people_controller and images_controller
and second question I have is, I have mention the dir to upload
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
am I suppose to see image uploaded to 'uploads/' folder of my c drive? I have storage :file on my avatar_uploader.rb file
please let me know what I am doing wrong? First time using Carrierwave here
any helps appreciate
Remove :multiple => true option from alerts/app/views/alerts/_victim.html.erb
Currently, file_field for avatar looks like:
<%= p.file_field :avatar, :multiple => true, class:'form-control input-normal' %>
Your file_field should look like:
<%= p.file_field :avatar, class:'form-control input-normal' %>
With the :multiple => true option :avatar field is send as Array upon form submission.
But as per your params hash, you are not expecting an array so you receive an Unpermitted parameters: avatar
I have a rails 4 app with strong parameters like:
def product_params
params.require(:product).permit(:title, :user_id, :info => [:color, :size])
end
But I'm getting the following error:
INSERT INTO "products" ("created_at", "info","title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Tue, 15 Apr 2014 01:30:31 UTC +00:00], ["info", [{"color"=>"Blue"}]], ["title", "Shoes"], ["updated_at", Tue, 15 Apr 2014 01:30:31 UTC +00:00]]
TypeError: can't cast Array to json: INSERT INTO "products" ("created_at", "info", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"