Reputation: 2763
I'm trying to put some state on my comments post who was created with polymorphic association. in my app/controllers/posts/comments_controller.rb
class Posts::CommentsController < CommentsController
before_action :set_commentable
#after_create :set_post_state
private
def set_commentable
@commentable = Post.find(params[:post_id])
set_post_state
debugger
end
def set_post_state
@commentable.update(state_id: params[:state_id])
end
end
As you can see I'm debugging for watch if the state_id was updated and it wasn't.
Started POST "/posts/2/comments" for ::1 at 2018-03-19 13:42:10 +0100
Processing by Posts::CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2AqfjRll/e7FVbnhZTA1DroG1pDPhdpqoiifLJkrIs6zqlmJnTuVVDIu7g0Bsh0L11dfzc/pbQVzdTJvZc0HUg==", "comment"=>{"content"=>"test", "state_id"=>"1"}, "commit"=>"Create Comment", "post_id"=>"2"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]]
CACHE (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "2"]]
(0.3ms) begin transaction
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE ("posts"."title" = 'Internet Explorer' AND "posts"."id" != 2) LIMIT 1
(0.1ms) commit transaction
Return value is: nil
[6, 15] in /Users/romenigld/ror_workspace/projects/news_city/app/controllers/posts/comments_controller.rb
6:
7: def set_commentable
8: @commentable = Post.find(params[:post_id])
9: set_post_state
10: debugger
=> 11: end
12:
13: def set_post_state
14: @commentable.update(state_id: params[:state_id])
15: end
(byebug) ap @commentable
/Users/romenigld/.rvm/gems/[email protected]/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
#<Post:0x007fd03266b958> {
:id => 2,
:title => "Internet Explorer",
:subtitle => "My sample of the subtitle",
:content => "A sample post about Internet Explorer",
:created_at => Mon, 12 Mar 2018 16:18:28 UTC +00:00,
:updated_at => Mon, 12 Mar 2018 16:18:28 UTC +00:00,
:author_id => 1,
:attachment => #<AttachmentUploader:0x007fd0326602d8 @model=#<Post id: 2, title: "Internet Explorer", subtitle: "My sample of the subtitle", content: "A sample post about Internet Explorer", created_at: "2018-03-12 16:18:28", updated_at: "2018-03-12 16:18:28", author_id: 1, attachment: "attachment.jpeg", state_id: nil>, @mounted_as=:attachment, @storage=#<CarrierWave::Storage::File:0x007fd032660148 @uploader=#<AttachmentUploader:0x007fd0326602d8 ...>>, @file=#<CarrierWave::SanitizedFile:0x007fd03265b288 @file="/Users/romenigld/ror_workspace/projects/news_city/public/uploads/post/attachment/2/attachment.jpeg", @original_filename=nil, @content_type=nil>, @versions={:thumb=>#<AttachmentUploader::Uploader70266096198580:0x007fd03265b1c0 @model=#<Post id: 2, title: "Internet Explorer", subtitle: "My sample of the subtitle", content: "A sample post about Internet Explorer", created_at: "2018-03-12 16:18:28", updated_at: "2018-03-12 16:18:28", author_id: 1, attachment: "attachment.jpeg", state_id: nil>, @mounted_as=:attachment, @parent_version=#<AttachmentUploader:0x007fd0326602d8 ...>, @storage=#<CarrierWave::Storage::File:0x007fd03265b008 @uploader=#<AttachmentUploader::Uploader70266096198580:0x007fd03265b1c0 ...>>, @file=#<CarrierWave::SanitizedFile:0x007fd032659fc8 @file="/Users/romenigld/ror_workspace/projects/news_city/public/uploads/post/attachment/2/thumb_attachment.jpeg", @original_filename=nil, @content_type=nil>, @versions={}>, :small_thumb=>#<AttachmentUploader::Uploader70266096186560:0x007fd03265b198 @model=#<Post id: 2, title: "Internet Explorer", subtitle: "My sample of the subtitle", content: "A sample post about Internet Explorer", created_at: "2018-03-12 16:18:28", updated_at: "2018-03-12 16:18:28", author_id: 1, attachment: "attachment.jpeg", state_id: nil>, @mounted_as=:attachment, @parent_version=#<AttachmentUploader:0x007fd0326602d8 ...>, @storage=#<CarrierWave::Storage::File:0x007fd032659d70 @uploader=#<AttachmentUploader::Uploader70266096186560:0x007fd03265b198 ...>>, @file=#<CarrierWave::SanitizedFile:0x007fd032658cb8 @file="/Users/romenigld/ror_workspace/projects/news_city/public/uploads/post/attachment/2/small_thumb_attachment.jpeg", @original_filename=nil, @content_type=nil>, @versions={}>}>,
:state_id => nil
}
nil
(byebug)
in app/controllers/comments_controller.rb
I add:
def comment_params
params.require(:comment).permit(:content, :state_id)
end
app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :author, class_name: "User"
belongs_to :commentable, polymorphic: true
belongs_to :state
app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :author, class_name: "User"
belongs_to :state
has_many :roles, dependent: :delete_all
has_many :comments, as: :commentable, dependent: :destroy
app/models/state.rb
class State < ActiveRecord::Base
def to_s
name
end
end
Upvotes: 0
Views: 32
Reputation: 2763
I put like @draganstankovic put a puts "Params: #{params}"
and then I observe the params was needed to put the comment params like this params[:comment][:state_id]
and now it's recording.
def set_post_state
puts "Params: #{params}"
@commentable.update!(state_id: params[:comment][:state_id])
end
Thank's for reply @draganstankovic.
Upvotes: 0
Reputation: 5436
My guess is that params[:state_id]
is nil but better not guess and instead put a puts
just before the update and check it out yourself:
13: def set_post_state
--> puts "Params: #{params}"
14: @commentable.update(state_id: params[:state_id])
15: end
Upvotes: 1