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 <%= 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 %>
.
. @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 if @notice.save
puts params.to_yaml
if !params[:notice][:active_comment_relationship][:commentee_id].nil? # THE OFFENDING LINE
@notice.create_comment(params[:notice][:active_comment_relationship][:commentee_id])
end
end
puts params.to_yaml will return the structure and values to your logging.
Otherwise, you are testing too generally for uncertain aspects: if !params[:notice][:active_comment_relationship][:commentee_id].nil?
params[:notice].blank?
....
unless params[:notice][:active_comment_relationship].blank?
....
unless params[:notice][:active_comment_relationship][:commentee_id].blank?
...
--- !ruby/hash:ActionController::Parameters
utf8: ✓
authenticity_token: nNWNdGOzKSu5gfDkB4x3AFIO1PQnIggn65uE//SGQNzK8Ub5AOtsShH3CLZd+oU7ic9WlA+F0RbsSVR6X1I78w==
callsign: bazzer
notice: !ruby/hash:ActionController::Parameters
latitude: '51.75253980933651'
longitude: '-1.39801025390625'
content: Hello everyone
commit: Drop
controller: notices
action: create
Completed 500 Internal Server Error in 31ms (ActiveRecord: 7.1ms)
NoMethodError (undefined method `[]' for nil:NilClass):
I have updated my code to use a form builder. It's all working great except for this one problem. The code is below. For some reason the fields_for is not working - as the params above shows there should be an active_comment_relationship parameter for notice but it isn't present. What's wrong with this code?:
<%= form_for(:notice, url: :notices, method: :post, html: { multipart: true, class: "comment_form" } ) do |f| %>
<%= hidden_field_tag :callsign, @character.callsign %>
<%= f.fields_for :active_comment_relationship do |ff| %>
<%= ff.label :commentee_id %>
<%= ff.hidden_field :commentee_id, value: notice.id %>
<% end %>
<%= f.hidden_field :latitude, value: notice.latitude, id: "comment_notice_latitude" %>
<%= f.hidden_field :longitude, value: notice.longitude, id: "comment_notice_longitude" %>
<%= f.text_area :content, rows: 1, id: "commentField-#{notice.id}", class: "comment_area" %>
<%= f.submit( "Post", class: 'btn btn-default btn-xs',
onclick: "return validateCommentForm('#commentField-#{notice.id}');" ) do %>
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
<% end %>
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
<% end %>