Created
April 25, 2016 03:36
-
-
Save hoasung01/9a7e428dece3084c77be8260bca6c57b to your computer and use it in GitHub Desktop.
multiple controllers in one views
This file contains 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
- mình có 2 controllers khác nhau: users_controller và contact_details_controller | |
- nhưng trong views/users/edit mình muốn thêm vào form new của contact_details thì hướng giải quyết ra sao nhỉ? | |
- mình có tham khảo một số cách làm thì dùng form_tag url_for đến controllers và action name của contact_details_controller | |
- nhưng hiện tại thì mình đã implement các action name cho mỗi controller xong hết rồi | |
- sample code của mình: | |
``` | |
class UserController < UserDashboardController | |
include Devise::Controllers::Helpers | |
include ContactDetailHelper | |
before_action :authorize_resource | |
after_action :verify_authorized, except: :index | |
helper DeviseHelper | |
def update | |
prev_unconfirmed_email = @user.unconfirmed_email if @user.respond_to?(:unconfirmed_email) | |
if @user.update_with_password(updated_params) | |
message = update_needs_confirmation?(@user, prev_unconfirmed_email) ? I18n.t('devise.registrations.update_needs_confirmation') : I18n.t('devise.registrations.updated') | |
flash[:notice] = message | |
sign_in :user, @user, bypass: true | |
redirect_to user_path | |
else | |
message = require_current_password? ? I18n.t('devise.passwords.need_current_password') : I18n.t('devise.failure.update_failed') | |
flash[:error] = message | |
clean_up_passwords @user | |
render :edit | |
end | |
end | |
def my_account | |
end | |
protected | |
def require_current_password? | |
# Set new password or update new email | |
!secure_params[:password].blank? || (!secure_params[:email].blank? && secure_params[:email] != @user.email) | |
end | |
def update_needs_confirmation?(resource, previous) | |
resource.respond_to?(:pending_reconfirmation?) && | |
resource.pending_reconfirmation? && | |
previous != resource.unconfirmed_email | |
end | |
def authorize_resource | |
@user = current_user | |
authorize @user | |
end | |
def clean_up_passwords(object) | |
object.clean_up_passwords if object.respond_to?(:clean_up_passwords) | |
end | |
private | |
def secure_params | |
params.require(:user).permit(:first_name, :last_name, :password, :current_password) | |
end | |
def updated_params | |
params.require(:user).permit(:password, :current_password) | |
end | |
end | |
``` | |
``` | |
class ContactDetailController < UserDashboardController | |
before_action :load_parent_and_build_objects | |
after_action :verify_authorized, except: :index | |
def new | |
end | |
def edit | |
end | |
def create | |
@contact_detail.attributes = secure_params | |
if @contact_detail.save | |
flash.notice = I18n.t('messages.creating_successful', object_name: I18n.t('contact_detail', scope: 'activerecord.models')) | |
redirect_to after_create_contact_detail_link | |
else | |
render :new | |
end | |
end | |
def update | |
if @contact_detail.update_attributes(secure_params) | |
flash.notice = flash.notice = I18n.t('messages.updating_successful', object_name: I18n.t('contact_detail', scope: 'activerecord.models')) | |
redirect_to smart_parent_contact_detail_link(:edit, @parent_object) | |
else | |
render :edit | |
end | |
end | |
protected | |
def load_parent_and_build_objects | |
@user = current_user | |
contactable_type = params[:contactable_type] | |
@parent_object ||= case contactable_type | |
when 'corporate_investor' | |
@user.corporate_investor | |
else | |
@user | |
end | |
if ['new', 'create'].include?(params[:action]) | |
@contact_detail = @parent_object.contact_detail | |
if @contact_detail.present? && @contact_detail.persisted? | |
redirect_to smart_parent_contact_detail_link(:edit, @parent_object) | |
else | |
authorize @parent_object, :create_contact_detail? | |
@contact_detail = @parent_object.build_contact_detail | |
end | |
else | |
authorize @parent_object, :contact_detail? | |
@contact_detail = @parent_object.contact_detail | |
end | |
end | |
def after_create_contact_detail_link | |
parent_object = @contact_detail.contactable | |
if parent_object.is_a?(CorporateInvestor) | |
new_corporate_investor_bank_account_path(parent_object) | |
else | |
smart_parent_contact_detail_link(:edit, parent_object) | |
end | |
end | |
def smart_parent_contact_detail_link(action, parent_object) | |
route_name = "#{action}_#{parent_object.model_name.singular_route_key}_contact_detail_path" | |
send_params = [] | |
send_params << parent_object unless parent_object.is_a?(User) | |
send(route_name, *send_params) | |
end | |
private | |
def secure_params | |
params.require(:contact_detail).permit(policy(@parent_object).permitted_contact_detail_attribute) | |
end | |
end | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment