error in comments_controller, module 10 - Evanto/qna GitHub Wiki
При добавлении комментария через форму коммент не постится, в терминале ошибка в comments_controller
load_commentable
:
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"commentable_id"=>"", "commentable_type"=>"", "body"=>"yuk"}, "commit"=>"Create"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 ["id", 41], ["LIMIT", 1](/Evanto/qna/wiki/"id",-41],-["LIMIT",-1)
Completed 500 Internal Server Error in 1011ms (ActiveRecord: 0.5ms)
если в rb:17:
@commentable = (params[:comment][:commentable_type]).constantize.find(params[:comment][:commentable_id])
NameError (wrong constant name ):
app/controllers/comments_controller.rb:17:in `load_commentable'
если в rb:18:
@commentable = commentable_type.classify.constantize.find(params[:commentable_id])
NameError (undefined local variable or method `commentable_type' for #<CommentsController:0x007fbac6eadde0>):
app/controllers/comments_controller.rb:18:in `load_commentable'
если в rb:17-18:
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
ActiveRecord::RecordNotFound (Couldn't find Comment without an ID):
app/controllers/comments_controller.rb:18:in `load_commentable'
Что-то не так с commentable_id
. Через rails console
коммент без commentable_id
создается, а с ним - нет (сама колонка commentable_id
существует, но в нее не пишутся данные при создании коммента через консоль):
irb(main):002:0> comment = Comment.create(id: 1, user_id: 3, body: "weg", commentable_type: "Question")
(0.1ms) BEGIN
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 ["id", 3], ["LIMIT", 1](/Evanto/qna/wiki/"id",-3],-["LIMIT",-1)
SQL (0.9ms) INSERT INTO "comments" ("id", "user_id", "body", "commentable_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" ["id", 1], ["user_id", 3], ["body", "weg"], ["commentable_type", "Question"], ["created_at", "2017-11-14 09:22:08.900105"], ["updated_at", "2017-11-14 09:22:08.900105"](/Evanto/qna/wiki/"id",-1],-["user_id",-3],-["body",-"weg"],-["commentable_type",-"Question"],-["created_at",-"2017-11-14-09:22:08.900105"],-["updated_at",-"2017-11-14-09:22:08.900105")
(0.4ms) COMMIT
=> #<Comment id: 1, user_id: 3, body: "weg", commentable_type: "Question", commentable_id: nil, created_at: "2017-11-14 09:22:08", updated_at: "2017-11-14 09:22:08">
irb(main):003:0> comment = Comment.create(id: 1, user_id: 3, body: "weg", commentable_type: "Question", commentable_id: 198)
(0.4ms) BEGIN
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 ["id", 3], ["LIMIT", 1](/Evanto/qna/wiki/"id",-3],-["LIMIT",-1)
SQL (0.8ms) INSERT INTO "comments" ("id", "user_id", "body", "commentable_type", "commentable_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" ["id", 1], ["user_id", 3], ["body", "weg"], ["commentable_type", "Question"], ["commentable_id", 198], ["created_at", "2017-11-14 09:23:16.310888"], ["updated_at", "2017-11-14 09:23:16.310888"](/Evanto/qna/wiki/"id",-1],-["user_id",-3],-["body",-"weg"],-["commentable_type",-"Question"],-["commentable_id",-198],-["created_at",-"2017-11-14-09:23:16.310888"],-["updated_at",-"2017-11-14-09:23:16.310888")
(0.3ms) ROLLBACK
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "comments_pkey"
DETAIL: Key (id)=(1) already exists.
: INSERT INTO "comments" ("id", "user_id", "body", "commentable_type", "commentable_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"
from (irb):3
Без передачи commentable_id
запись в таблице Comments создалась (с commentable_id: nil
):
irb(main):004:0> Comment.first
Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."created_at" ASC LIMIT $1 ["LIMIT", 1](/Evanto/qna/wiki/"LIMIT",-1)
=> #<Comment id: 1, user_id: 3, body: "weg", commentable_type: "Question", commentable_id: nil, created_at: "2017-11-14 09:22:08", updated_at: "2017-11-14 09:22:08">
Не загружает commentable
(Question или Answer).
Проверка колонок талицы Comments:
ubuntu@rails-dev-box:~/www/qna$ rails c
irb(main):001:0> Comment.column_names
=> ["id", "user_id", "body", "commentable_type", "commentable_id", "created_at", "updated_at"]