Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Created February 2, 2013 22:41
Show Gist options
  • Save brandon-beacher/4699583 to your computer and use it in GitHub Desktop.
Save brandon-beacher/4699583 to your computer and use it in GitHub Desktop.
= simple_form_for @profile, url: my_membership_path, html: { class: "form-horizontal" } do |form|
= form.simple_fields_for @profile.customer do |fields|
= fields.input :first_name
= fields.input :last_name, label: false
= form.simple_fields_for @profile.user do |fields|
= fields.input :email
= fields.input :password
= form.simple_fields_for @profile.customer do |fields|
= fields.input :zip_code
.form-actions
= form.button :submit, class: "btn btn-primary"
class MyMembershipsController < ApplicationController
respond_to :html
def edit
@profile = Profile.find(current_customer)
end
def update
@profile = Profile.find(current_customer)
if @profile.update_attributes(profile_params)
flash[:notice] = t("flash.my_membership.update.notice")
respond_with current_customer, location: my_subscriptions_path
else
flash[:alert] = t("flash.my_membership.update.alert")
render "edit"
end
end
private
def profile_params
params.require(:profile).permit(
customer: [
:first_name,
:last_name,
:zip_code ],
user: [
:email,
:password ] )
end
end
class Profile
include ActiveModel::Model
attr_reader :customer
attr_reader :user
def self.find(customer)
new(customer, customer.user)
end
def initialize(customer, user)
@customer = customer
@user = user
end
def persisted?
true
end
def update_attributes(params)
customer.with_transaction_returning_status do
customer.update_attributes(params[:customer]) &&
user.update_attributes(params[:user])
end
end
end
@stevenharman
Copy link

Profile#update_attributes feels awkward. I realize you're trying to stick with Rail-y looking signatures, but the _attributes suffix is distracting - it has nothing to do with the domain, and is a mere implementation detail of how Profile is "updated."

I think I'd opt to drop the _attributes suffix.

@alindeman
Copy link

As a side note, in Rails 4, you can use Model#update instead of #update_attributes. Using update here is both more and less Rails-y at the same time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment