ianroberts131 / coalesce-to-give

0 stars 0 forks source link

Add social profiles to user and nonprofit show pages #23

Closed ianroberts131 closed 8 years ago

ianroberts131 commented 8 years ago

Eliot,

I was considering writing a blog post about how I resolved the fields_for not displaying social profiles if the social_profile for that user had not yet been created. Unlike nonprofits, the social profile is not created at sign-up, so I couldn't use the build feature and handle this in the user registration controller. All of the solutions I found online suggested putting SocialProfile.new in the fields_for, i.e

<%= f.fields_for :social_profile, SocialProfile.new do |social_profile_form| %>

The problem with this solution is that once I create a path to one (or many) of the social networks, it will be overwritten the next time I go in to edit the profile. I thought a cleaner way was to conditionally create a new social profile only if one didn't exist yet, like this:

<% @user.social_profile ||= SocialProfile.new %>
<%= f.fields_for :social_profile do |social_profile_form| %>

I figure someone else out there might have the same issue, but if there is a write up out there it isn't easy to find. Do you think the way I handled it is a good solution? And if so, do you think it's blog post worthy?