Rails: "new or edit" path helper? -
is there simple , straightforward way provide link in view either create resource if doesn't exist or edit existing on if does?
ie:
user has_one :profile currently doing like...
-if current_user.profile? = link_to 'edit profile', edit_profile_path(current_user.profile) -else = link_to 'create profile', new_profile_path this ok if it's way, i've been trying see if there's "rails way" like:
= link_to 'manage profile', new_or_edit_path(current_user.profile) is there nice clean way that? view equivalent of model.find_or_create_by_attribute(....)
write helper encapsulate more complex part of logic, views can clean.
# profile_helper.rb module profilehelper def new_or_edit_profile_path(profile) profile ? edit_profile_path(profile) : new_profile_path(profile) end end now in views:
link_to 'manage profile', new_or_edit_profile_path(current_user.profile)
Comments
Post a Comment