Ruby on Rails
Sunday, August 2, 2015
@Colin is right. I think here that you should be asking, how can I learn the params returned?
To debug, modify. That is what I have used, others may have better suggestions:
Test each aspect for the moment to ascertain missing values:
unless
On Sunday, August 2, 2015 at 4:15:18 PM UTC-4, Bazley wrote:
-- To debug, modify. That is what I have used, others may have better suggestions:
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?
Test each aspect for the moment to ascertain missing values:
unless
params[:notice].blank?
....
unless
params[:notice][:active_comment_relationship ].blank?
....
unless
params[:notice][:active_comment_relationship][
:commentee_id ]
.blank?
...
On Sunday, August 2, 2015 at 4:15:18 PM UTC-4, Bazley 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?
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/df3fb306-48f0-43a7-80a7-6dc4c31580a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment