lundi 21 mars 2016

Rails save contact details to another table

In my rails app, during checkout, the user contact details are pulled from user profile by default, but if for example the phone_number is empty in profile table, the field will be left empty. Once checkout if the user change or maintain the data, the data will be saved to contact_details table and user will be redirect to PayPal, in case user cancel the payment in PayPal, they will return back to checkout page and existing data are pulled from previously saved contact_details table.

Now the issues is, how can I can display user profile in the fields, but save the data to contact_details? Following were my code

        <%= form_for(@invoice) do |f| %>
            <div class="form-block">
              <div class="form-group col-md-6 firstname">
                <label for="sec">First Name:</label>
                <input type="text" placeholder='First Name' class="form-control input-pay" id="sec" name="contact_detail[first_name]" value="<%= @current_user.profile.first_name %>">
              </div>

    <div class="form-group col-md-6 firstname">
        <label for="sec">Email:</label>
        <input type="text" placeholder='Email' class="form-control input-pay" id="sec" name="contact_detail[email]" value="<%= @current_user.email %>">
     </div>

    </div>
<% end %>

controller:

def new

        @invoice = Invoice.new
        if request.post?

            @invoice.booking_date = params[:datepicker]
            @invoice.product_id = params["product-id"]
            @invoice.variants = params[:variant]
            if !user_signed_in?
                session[:pending_invoice] = @invoice
                return redirect_to new_user_session_path
            end

            set_extra_params_for_new

            @invoice_params = {}
            @contact_detail_params = {}

        else

            if (session[:pending_invoice].present?) && (session[:pending_invoice].instance_of? Invoice)

                @invoice = session[:pending_invoice]
                set_extra_params_for_new

                @invoice_params = {}
                @contact_detail_params = {}
                session[:pending_invoice] = nil
            else
                @invoice.booking_date = params[:invoice][:booking_date]
                @invoice.product_id = params[:invoice][:product_id]
                @invoice.coupon_id = params[:invoice][:coupon_id]
                @invoice.coupon_amounts = params[:invoice][:coupon_amounts]
                @coupon_amounts_with_user_currency = params[:invoice][:coupon_amounts_with_user_currency]
                @invoice.variants = params[:variant]

                set_extra_params_for_new

                @invoice_params = params[:invoice]
                @contact_detail_params = params[:contact_detail]
            end
        end

        set_invoice_currency_attributes(@invoice)

        gon.invoice = @invoice
        gon.real_total_with_currency = @invoice.real_total_with_currency
        gon.real_total = @invoice.real_total

    end

private



        def contact_detail_params
            params.require(:contact_detail).permit(:first_name, :last_name, :email, :phone_number, :message)
        end

Aucun commentaire:

Enregistrer un commentaire