I am using this in my controller:
def step_params
params.require(#type.underscore.to_sym).permit(
:id, :name, :note, :position, :institution_id, :protocol_id, :sequence_id,:orientation_id,
step_item_attributes: [:id, :note, :name, :position, :institution_id, :sequence_id, :orientation_id, :_destroy ],
step_list_attributes: [:id, :note, :name, :position, :institution_id, :sequence_id, :orientation_id, :_destroy ])
end
And see this in the server log after a form with nested attributes is submitted:
Processing by StepsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xm6oMMJ2PLXhHfKS1RkIzG5LrCUAY6vLOF+e9XHgBE4=", "step_list"=>{"name"=>"bob bob", "note"=>"", "step_items_attributes"=>{"1411264481612"=>{"name"=>"", "orientation_id"=>"1", "sequence_id"=>"1", "note"=>"a note", "_destroy"=>"false"}}}, "commit"=>"Create Step list", "type"=>"StepList"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Unpermitted parameters: step_items_attributes
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "steps" ("created_at", "institution_id", "name", "note", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", "2014-09-21 01:54:49.736556"], ["institution_id", 18], ["name", "bob bob"], ["note", ""], ["updated_at", "2014-09-21 01:54:49.736556"]]
(37.5ms) commit transaction
Redirected to http://localhost:3000/steps/54
Completed 302 Found in 47ms (ActiveRecord: 38.7ms)
Looks to me like "Unpermitted parameters: step_items_attributes"
... is the problem.
Why does my permit method not allow the step_items_attributes hash? How could I figure out what other notation might work?
Your permitted Items sets step_item_attributes but you trying to pass step_items_attributes (items) with an 's'. so that's why you get Unpermitted parameters: step_items_attributes
Related
Using of JSON_ARRAYAGG does not working for me
1) [Code: -104, SQL State: 42601] An unexpected token "ORDER" was found following "CITY)
". Expected tokens may include: ")".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.28.11
2) [Code: -727, SQL State: 56098] An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-104", SQLSTATE "42601" and message tokens "ORDER|CITY)
|)".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.28.11
I want an output like this
{"id":901, "name": "Hansi", "addresses" :
[
{ "address":"A", "city":"B"},
{ "address":"C", "city":"D"}
]
}
I am using IBM DB2 11.1 for Linux, UNIX and Windows.
values (
json_array(
select json_object ('ID' value ID,
'NAME' value NAME,
'ADDRESSES' VALUE JSON_ARRAYAGG(
JSON_OBJECT('ADDRESS' VALUE ADDRESS,
'CITY' VALUE CITY)
ORDER BY ADDRESS)
)
FROM CUSTOMER
JOIN CUSTOMER_ADDRESS ON ADDRESS_CUSTOMER_ID = ID
GROUP BY ID, NAME
FORMAT JSON
));
Used tables are:
CUSTOMER - ID (INT), NAME (VARCHAR64)
ADDRESS - ADDRESS (VARCHAR64), CITY (VARCHAR64)
I would like to change the database in my existing project from mysql to postgresql.
I have configured the database, I have regenerated the migrations which work, but the problem appears in the fixtures.
when trying to load fixtures an error appears like this:
An exception occurred while executing 'INSERT INTO user (nickname, password, id, created_at, updated_at, email) VALUES (?, ?, ?, ?, ?, ?)' with params ["user", "$2y$13$rgHtT56Vlk2
avmf3gX2W7.QYcQ5d6AXRzr41ebRMGfxREqLZQfsTG", "017c4562-d487-ddff-c303-108c1916d6dd", "2021-10-03 11:01:16", "2021-10-03 11:01:16", "user#user.pl"]:
SQLSTATE[42601]: Syntax error: 7 BŁĄD: błąd składni w lub blisko "user"
LINE 1: INSERT INTO user (nickname, password, id, created_at, update... ,
this is a problem possibly caused by entity manager generating the mysql dialect instead of postgres dialect.
a similar error occurs during the get shot under the user entity:
"hydra:description": "An exception occurred while executing 'SELECT u0_.nickname AS nickname_0, u0_.password AS password_1, u0_.id AS id_2, u0_.created_at AS created_at_3, u0_.updated_at AS updated_at_4, u0_.email AS email_5 FROM user u0_':\n\nSQLSTATE[42703]: Undefined column: 7 BŁĄD: kolumna u0_.nickname nie istnieje\nLINE 1: SELECT u0_.nickname AS nickname_0, u0_.password AS password_.
Here is my doctrine.yaml configuration:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
charset: utf8
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
Could someone help me get rid of the bug? :)
I am facing an issue while learning Elixir & Ecto. The idea is to build a standard posts/comments page to understand how the basics work. I am at a point where I have schemas defined, a migration written and encounter an error when trying to insert data into the database (PostgreSQL) via the Repo. I have done a fair deal of web searching and documentation reading, which leads me to believe it's a scenario that should just work and I am making a stupid mistake somewhere, which I just can't see.
They are defined as follows:
lib/hello/schemas.ex
defmodule Hello.PostAuthor do
use Ecto.Schema
schema "post_authors" do
field :name, :string
end
end
defmodule Hello.CommentAuthor do
use Ecto.Schema
schema "comment_authors" do
field :name, :string
end
end
defmodule Hello.Comment do
use Ecto.Schema
schema "comments" do
has_one :author, Hello.CommentAuthor
field :date, :date
field :body, :string
end
end
defmodule Hello.Post do
use Ecto.Schema
schema "posts" do
has_one :author, Hello.PostAuthor
field :date, :date
field :body, :string
has_many :comments, Hello.Comment
end
end
as you can see, I have two fields with :date type - on post and comment schemas. The corresponding migration is as follows:
defmodule Hello.Repo.Migrations.CreatePosts do
use Ecto.Migration
def change do
create table(:post_authors) do
add :name, :string
end
create table(:comment_authors) do
add :name, :string
end
create table(:comments) do
add :author, references(:comment_authors)
add :date, :date
add :body, :string
end
create table(:posts) do
add :author, references(:post_authors), null: false
add :date, :date
add :body, :string
add :comments, references(:comments)
timestamps()
end
end
end
Now, when I start iex -S mix I can successfully create all structs:
iex(1)> post_author = %Hello.PostAuthor{name: "John"}
%Hello.PostAuthor{
__meta__: #Ecto.Schema.Metadata<:built, "post_authors">,
id: nil,
name: "John"
}
iex(2)> comment_author = %Hello.PostAuthor{name: "Adam"}
%Hello.PostAuthor{
__meta__: #Ecto.Schema.Metadata<:built, "post_authors">,
id: nil,
name: "Adam"
}
iex(3)> comment = %Hello.Comment{author: comment_author, date: ~D[2019-01-01], body: "this is a comment"}
%Hello.Comment{
__meta__: #Ecto.Schema.Metadata<:built, "comments">,
author: %Hello.PostAuthor{
__meta__: #Ecto.Schema.Metadata<:built, "post_authors">,
id: nil,
name: "Adam"
},
body: "this is a comment",
date: ~D[2019-01-01],
id: nil
}
iex(4)> post = %Hello.Post{author: post_author, date: ~D[2019-01-01], body: "this is a post", comments: [comment]}
%Hello.Post{
__meta__: #Ecto.Schema.Metadata<:built, "posts">,
author: %Hello.PostAuthor{
__meta__: #Ecto.Schema.Metadata<:built, "post_authors">,
id: nil,
name: "John"
},
body: "this is a post",
comments: [%Hello.Comment{
__meta__: #Ecto.Schema.Metadata<:built, "comments">,
author: %Hello.PostAuthor{
__meta__: #Ecto.Schema.Metadata<:built, "post_authors">,
id: nil,
name: "Adam"
},
body: "this is a comment",
date: ~D[2019-01-01],
id: nil
}],
date: ~D[2019-01-01],
id: nil
}
The problem arises when I call Hello.Repo.insert(post) (where post is the struct representing the Hello.Post schema). I receive what looks like serialization error:
iex(8)> Hello.Repo.insert(post) [debug] QUERY OK db=0.1ms
begin []
[debug] QUERY ERROR db=1.6ms
INSERT INTO "posts" ("body","date") VALUES ($1,$2) RETURNING "id" ["this is a post", ~D[2019-01-01]]
[debug] QUERY OK db=0.1ms
rollback []
** (DBConnection.EncodeError) Postgrex expected a binary, got ~D[2019-01-01]. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
(postgrex) lib/postgrex/type_module.ex:897: Postgrex.DefaultTypes.encode_params/3
(postgrex) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3
(db_connection) lib/db_connection.ex:1148: DBConnection.encode/5
(db_connection) lib/db_connection.ex:1246: DBConnection.run_prepare_execute/5
(db_connection) lib/db_connection.ex:540: DBConnection.parsed_prepare_execute/5
(db_connection) lib/db_connection.ex:533: DBConnection.prepare_execute/4
(postgrex) lib/postgrex.ex:198: Postgrex.query/4
(ecto_sql) lib/ecto/adapters/sql.ex:666: Ecto.Adapters.SQL.struct/10
(ecto) lib/ecto/repo/schema.ex:651: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:262: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:916: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(ecto_sql) lib/ecto/adapters/sql.ex:898: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection) lib/db_connection.ex:1415: DBConnection.run_transaction/4
This is where I am lost. Both the schema and the migration are expecting a :date . I believe that ~D[2019-01-01] is a date. PostgreSQL defines date as a 4 byte binary value. I am expecting Ecto.Adapters.Postgres to translate elixir date struct into the Postgres binary value. This is not happening. Why?
Struct itself is just raw data. You should go through Ecto.Changeset as shown in the documentation, specifically to all types to be cast to the respective DB types with Ecto.Changeset.cast/4.
The conversion will be done automagically, but you need to explicitly call cast/4 (hence the Changeset,) otherwise the adapter has no idea of how to convert your ecto types.
This is my image model:
class Image < ApplicationRecord
belongs_to :user
mount_uploader :avatar, AvatarUploader
end
I can't even create an image in console without a user? Did this change from Rails 4?
Image.create
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> #<Image id: nil, user_id: nil, title: nil, created_at: nil, updated_at: nil, avatar: nil>
2.2.4 :018 > Image.create(title: "hi")
(0.3ms) begin transaction
(0.1ms) rollback transaction
=> #<Image id: nil, user_id: nil, title: "hi", created_at: nil, updated_at: nil, avatar: nil>
2.2.4 :019 > u = User.create
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2016-03-17 04:27:09 UTC], ["updated_at", 2016-03-17 04:27:09 UTC]]
(8.6ms) commit transaction
=> #<User id: 6, name: nil, created_at: "2016-03-17 04:27:09", updated_at: "2016-03-17 04:27:09">
2.2.4 :020 > u.images << Image.create
(0.1ms) begin transaction
(0.0ms) rollback transaction
(0.0ms) begin transaction
SQL (0.3ms) INSERT INTO "images" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) [["user_id", 6], ["created_at", 2016-03-17 04:27:16 UTC], ["updated_at", 2016-03-17 04:27:16 UTC]]
(7.8ms) commit transaction
Image Load (0.2ms) SELECT "images".* FROM "images" WHERE "images"."user_id" = ? [["user_id", 6]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Image id: 3, user_id: 6, title: nil, created_at: "2016-03-17 04:27:16", updated_at: "2016-03-17 04:27:16", avatar: nil>]>
I can't get my factories setup:
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "jeff#{n}" }
after(:build) do |user, eval|
user.images << build(:image)
end
end
factory :image do
avatar File.open(File.join(Rails.root, '/spec/support/images/blueapron.jpg'))
end
end
These are my error messsages:
* image - Validation failed: User must exist (ActiveRecord::RecordInvalid)
from /Users/Jwan/.rvm/gems/ruby-2.2.4/gems/factory_girl-4.5.0/lib/factory_girl/linter.rb:4:in `lint!'
from /Users/Jwan/.rvm/gems/ruby-2.2.4/gems/factory_girl-4.5.0/lib/factory_girl.rb:59:in `lint'
from /Users/Jwan/Dropbox/programming/rails/carrierwave_s3/spec/support/factory_girl.rb:10:in `block (2
Yes it changed in rails 5. If you need previous behavior, add optional: true to the belongs_to declaration.
I have the following form:
= form_for([current_user,#company], :remote => true) do |f|
-if #company.errors.any?
#error_explanation
%h2= "#{pluralize(#company.errors.count, "error")} prohibited this company from being saved:"
%ul
- #company.errors.full_messages.each do |msg|
%li= msg
=f.label :name
=f.text_field :name
=f.label :address
=f.text_area :address, :rows => 3, :cols => 5
=f.label :phone_number
=f.text_field :phone_number
.actions
= f.submit 'Save'
When I click the save button I can see the folllowing in my server log:
Started POST "/users/1/companies" for 127.0.0.1 at 2012-04-04 21:27:50 +0700
Processing by CompaniesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6ZH9hAUuf5ZTCf8Loc4M/IIl/Etzm7uDGoYbIgCTvhI=", "company"=>{"name"=>"test", "address"=>"test", "phone_number"=>"5454543"}, "commit"=>"Save", "user_id"=>"1"}
(0.2ms) BEGIN
SQL (25.2ms) INSERT INTO "companies" ("address", "name", "phone_number", "url", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["address", "test"], ["name", "test"], ["phone_number", "5454543"], ["url", nil], ["user_id", nil]]
(1.6ms) COMMIT
Rendered companies/create.js.erb (0.7ms)
Completed 200 OK in 41ms (Views: 8.1ms | ActiveRecord: 27.0ms)
Started POST "/users/1/companies" for 127.0.0.1 at 2012-04-04 21:27:50 +0700
Processing by CompaniesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6ZH9hAUuf5ZTCf8Loc4M/IIl/Etzm7uDGoYbIgCTvhI=", "company"=>{"name"=>"test", "address"=>"test", "phone_number"=>"5454543"}, "commit"=>"Save", "user_id"=>"1"}
(0.7ms) BEGIN
SQL (2.7ms) INSERT INTO "companies" ("address", "name", "phone_number", "url", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["address", "test"], ["name", "test"], ["phone_number", "5454543"], ["url", nil], ["user_id", nil]]
(12.8ms) COMMIT
Rendered companies/create.js.erb (0.1ms)
Completed 200 OK in 30ms (Views: 10.5ms | ActiveRecord: 16.1ms)
Which means the form is being submitted twice.
I have removed the assets folder from my public directory. I have also checked the html rendered on the page and there is no double inclusion of jquery or other dependencies.
Why is it being submitted twice?
i had run rake assets:precompile and was running my server in development mode. solution was to rake assets:clean and restart the sever
My workaround for this:
rake assets:precompile
with //= require jquery_ujs in your app/assets/javascripts/application.js
After the rake, remove the line from app/assets/javascripts/application.js and start the server.
Now it should work as expected.