notice.rb:
has_one :active_comment_relationship, class_name:
“Commentrelationship”,
foreign_key: “commenter_id”,
dependent: :destroy
has_one :supernotice, through: :active_comment_relationship, source:
:
commentee
accepts_nested_attributes_for :active_comment_relationship
_notice.html.erb:
<%= form_tag( {controller: "notices", action: "create"}, method:
“post”,
class: “comment_form”, multipart: true ) do %>
<%= hidden_field_tag :callsign, @character.callsign %>
<%= hidden_field_tag
“notice[active_comment_relationship][commentee_id]”, notice.id %>
.
.
notices_controller.rb:
@notice = @character.notices.build(notice_params)
if @notice.save
if
!params[:notice][:active_comment_relationship][:commentee_id].nil? #
THE OFFENDING LINE
@notice.create_comment(params[:notice][:active_comment_relationship
][:commentee_id])
end
end
def notice_params
params.require(:notice).permit( :content, :picture, :latitude, :
longitude, active_comment_relationship_attributes: [:commentee_id] )
end
The code above gives the following error:
undefined method `[]’ for nil:NilClass
Sometimes a notice is submitted with a blank/nil :commentee_id, so I
would
have thought
!params[:notice][:active_comment_relationship][:commentee_id].
nil?
would have worked. How do I write this correctly? How do you correctly
access nested parameters?