Ruby on Rails Sunday, August 2, 2015

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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f1c0a76e-2e38-4577-b830-5a97e596a016%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment