Ruby on Rails Sunday, August 2, 2015

On 2 August 2015 at 21:15, Bazley <1975jmp@gmail.com> wrote:
> 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?

Are you sure it is not active_comment_relationship that is nil? Have
a look in the log and see what it says for params. If you think it is
ok copy/paste the log for the complete transaction here.

Colin

--
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/CAL%3D0gLsaaUSztQ%2BBdLJQdCv5z2cPecHaRwMPLVB%3Dv4yHM4rypA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment