Created
February 2, 2013 22:41
-
-
Save brandon-beacher/4699583 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| = 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
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
Profile#update_attributesfeels awkward. I realize you're trying to stick with Rail-y looking signatures, but the_attributessuffix is distracting - it has nothing to do with the domain, and is a mere implementation detail of howProfileis "updated."I think I'd opt to drop the
_attributessuffix.