Advertisement
Guest User

Untitled

a guest
Aug 12th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. def update
  2. form = choose_form
  3. if form.validate(params_type)
  4. form.save
  5. redirect_to action: :edit
  6. else
  7. render :edit
  8. end
  9. end
  10.  
  11. class Users::ProfilesController < ApplicationController
  12. before_action :authenticate_user!
  13. before_action :set_user
  14.  
  15. def billing_form
  16. address = @user.billing_address || Address.new
  17. AddressForm.new(address)
  18. end
  19.  
  20. def shipping_form
  21. address = @user.shipping_address || Address.new
  22. AddressForm.new(address)
  23. end
  24.  
  25. def edit
  26. #сюдя приходят предзаполненные данные
  27. @billing_address ||= billing_form
  28. @shipping_address ||= shipping_form
  29. end
  30.  
  31. #Если не разбивать на 2 метода при валидации ошибка -
  32. #First argument in form cannot contain nil or be empty
  33. #= form_for @billing_address, as: :billing, url: {action: 'update'} do |f|
  34.  
  35. def update
  36. if params[:billing]
  37. update_billing
  38. else
  39. update_shipping
  40. end
  41. end
  42.  
  43. def update_billing
  44. @billing_address = choose_form
  45. if @billing_address.validate(params_type)
  46. @billing_address.save
  47. redirect_to action: :edit
  48. else
  49. @shipping_address ||= shipping_form
  50. render :edit
  51. end
  52. end
  53.  
  54. def update_shipping
  55. @shipping_address = choose_form
  56. if @shipping_address.validate(params_type)
  57. @shipping_address.save
  58. redirect_to action: :edit
  59. else
  60. @billing_address ||= billing_form
  61. render :edit
  62. end
  63. end
  64.  
  65. def create
  66. form = AddressForm.new(Address.new)
  67. if form.validate(params_type)
  68. form.save
  69. assign_address(form)
  70. @user.save
  71. redirect_to action: :edit
  72. else
  73. render :edit
  74. end
  75. end
  76.  
  77. def update_password
  78. @user = User.find(current_user.id)
  79. if @user.update_with_password(profile_params)
  80. sign_in @user, :bypass => true
  81. redirect_to root_path
  82. else
  83. render 'edit'
  84. end
  85. end
  86.  
  87. private
  88. def choose_form
  89. billing? ? billing_form : shipping_form
  90. end
  91.  
  92. def assign_address(form)
  93. billing? ? @user.billing_address = form.model : @user.shipping_address = form.model
  94. end
  95.  
  96. def billing?
  97. params[:billing].present?
  98. end
  99.  
  100. def params_type
  101. params[billing? ? :billing : :shipping]
  102. end
  103.  
  104. def profile_params
  105. params.require(:user).
  106. permit(:email, :password, :password_confirmation, :current_password)
  107. end
  108.  
  109. def set_user
  110. @user = current_user
  111. end
  112. end
  113.  
  114. class AddressForm < Reform::Form
  115.  
  116. property :first_name
  117. property :last_name
  118. property :address
  119. property :city
  120. property :country_id
  121. property :zipcode
  122. property :phone
  123.  
  124. validates :first_name, :last_name, :address, :zipcode, :city, :phone, presence: true
  125. end
  126.  
  127. .row
  128. .col-md-6
  129. = form_for @billing_address, as: :billing, url: {action: 'update'} do |f|
  130. %h3= t('checkout.billing_address')
  131. %hr.style3
  132. = render 'static/error_messages', target: @billing_address
  133. = render 'address/form', f: f
  134. = f.submit t('page.save_button'), class: 'btn btn-primary'
  135.  
  136. .col-md-6
  137. = form_for @shipping_address, as: :shipping, url: {action: 'update'} do |f|
  138. %h3= t('checkout.shipping_address')
  139. %hr.style3
  140. = render 'static/error_messages', target: @shipping_address
  141. = render 'address/form', f: f
  142. = f.submit t('page.save_button'), class: 'btn btn-primary'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement