Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def update
- form = choose_form
- if form.validate(params_type)
- form.save
- redirect_to action: :edit
- else
- render :edit
- end
- end
- class Users::ProfilesController < ApplicationController
- before_action :authenticate_user!
- before_action :set_user
- def billing_form
- address = @user.billing_address || Address.new
- AddressForm.new(address)
- end
- def shipping_form
- address = @user.shipping_address || Address.new
- AddressForm.new(address)
- end
- def edit
- #сюдя приходят предзаполненные данные
- @billing_address ||= billing_form
- @shipping_address ||= shipping_form
- end
- #Если не разбивать на 2 метода при валидации ошибка -
- #First argument in form cannot contain nil or be empty
- #= form_for @billing_address, as: :billing, url: {action: 'update'} do |f|
- def update
- if params[:billing]
- update_billing
- else
- update_shipping
- end
- end
- def update_billing
- @billing_address = choose_form
- if @billing_address.validate(params_type)
- @billing_address.save
- redirect_to action: :edit
- else
- @shipping_address ||= shipping_form
- render :edit
- end
- end
- def update_shipping
- @shipping_address = choose_form
- if @shipping_address.validate(params_type)
- @shipping_address.save
- redirect_to action: :edit
- else
- @billing_address ||= billing_form
- render :edit
- end
- end
- def create
- form = AddressForm.new(Address.new)
- if form.validate(params_type)
- form.save
- assign_address(form)
- @user.save
- redirect_to action: :edit
- else
- render :edit
- end
- end
- def update_password
- @user = User.find(current_user.id)
- if @user.update_with_password(profile_params)
- sign_in @user, :bypass => true
- redirect_to root_path
- else
- render 'edit'
- end
- end
- private
- def choose_form
- billing? ? billing_form : shipping_form
- end
- def assign_address(form)
- billing? ? @user.billing_address = form.model : @user.shipping_address = form.model
- end
- def billing?
- params[:billing].present?
- end
- def params_type
- params[billing? ? :billing : :shipping]
- end
- def profile_params
- params.require(:user).
- permit(:email, :password, :password_confirmation, :current_password)
- end
- def set_user
- @user = current_user
- end
- end
- class AddressForm < Reform::Form
- property :first_name
- property :last_name
- property :address
- property :city
- property :country_id
- property :zipcode
- property :phone
- validates :first_name, :last_name, :address, :zipcode, :city, :phone, presence: true
- end
- .row
- .col-md-6
- = form_for @billing_address, as: :billing, url: {action: 'update'} do |f|
- %h3= t('checkout.billing_address')
- %hr.style3
- = render 'static/error_messages', target: @billing_address
- = render 'address/form', f: f
- = f.submit t('page.save_button'), class: 'btn btn-primary'
- .col-md-6
- = form_for @shipping_address, as: :shipping, url: {action: 'update'} do |f|
- %h3= t('checkout.shipping_address')
- %hr.style3
- = render 'static/error_messages', target: @shipping_address
- = render 'address/form', f: f
- = f.submit t('page.save_button'), class: 'btn btn-primary'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement