0

Thank you for taking the time to possibly help me.

I would like to be able to save the current_user devise data when a client creates a new order. Following everything i can find i managed to get the user_id to save but not the things i need like email. I will be keeping a close eye on this post.

For clarification, I need to save the account data when the form is submitted to avoid haveing the user add in the data again like there name email ect. This data is already saved on the devise user account. So i would like to submit that with the form thats submitted.

I have seen things like this in the orders_controller.rb

def create
  @order = current_user
end

But it simply does not save any of that data. You can see the console output when i do this at the bottom of this post. It is nil.

Models:

User.rb

    class User < ActiveRecord::Base
      has_many :orders
      # Include default devise modules. Others available are:
      # , :lockable, :timeoutable and :omniauthable , :registerable
      devise :database_authenticatable,
             :recoverable, :rememberable, :trackable, :validatable, :confirmable
      attr_accessor :email
    end

order.rb

    class Order < ActiveRecord::Base
      belongs_to :user
      attr_accessor :email
    end

Controllers:

orders_controller.rb

    class OrdersController < ApplicationController
      before_filter :authenticate_user!
      def new
        @order = Order.new
      end

      def create
        @order = Order.new(order_params)
        @order = current_user.orders.new(params[:email])
        if @order.save
          redirect_to dconfirmation_path
        end
      end

      def order_params
        params.require(:order).
          permit(
            :email,
            :delivery_name,
            :company_name,
            :delivery_address1,
            :delivery_address2,
            :delivery_address3,
            :delivery_city,
            :delivery_postcode,
            :delivery_country,
            :phone,
            :package_contents,
            :description_content,
            :restricted_items,
            :terms_conditions,
            :insurance,
            :contents_value,
            :cf_reference,
            :reference_number
            )
      end
      def show
        @user = User.find(params[:id])
      end

      def confirmation
      end

    end

Now all saves the fields from the form and the user_id fine but i need to be able to show who created the order by email. If i go to the console and do Order.Last this is the data that saves for example:

      Order Load (0.6ms)  SELECT  "orders".* FROM "orders"  ORDER BY "orders"."id" DESC LIMIT 1
    => #<Order id: 13, delivery_name: nil, company_name: nil, delivery_address1: nil, delivery_address2: nil, delivery_address3: nil, delivery_city: nil, delivery_postcode: nil, delivery_country: nil, phone: nil, package_contents: nil, description_content: nil, restricted_items: nil, terms_conditions: nil, insurance: nil, contents_value: nil, cf_reference: nil, reference_number: nil, created_at: "2015-06-30 20:54:22", updated_at: "2015-06-30 20:54:22", user_id: 2, name: nil, address_line_1: nil, address_line_2: nil, postcode: nil, city: nil, country: nil, email: nil>

Of course i dont want the

email: nil>

to be nil The current_user has a email assigned to that account that i want to be saved there

-Optional data ?

Migrations:

20150630090050_add_user_id_to_orders.rb

class AddUserIdToOrders < ActiveRecord::Migration
  def change
    add_column :orders, :user_id, :integer
    add_column :orders, :name, :string
    add_column :orders, :address_line_1, :string
    add_column :orders, :address_line_2, :string
    add_column :orders, :postcode, :string
    add_column :orders, :city, :string
    add_column :orders, :country, :string
  end
end

add_email_to_orders.rb

class AddEmailToOrders < ActiveRecord::Migration
  def change
    add_column :orders, :email, :string
  end
end

ps. I will provide any Additional details required.

Morphasis
  • 1,343
  • 1
  • 12
  • 25
  • I really have tried looking. Please go easy on me i'm new to rails and still learning. – Morphasis Jun 30 '15 at 21:21
  • possible duplicate of [Access current\_user in model](http://stackoverflow.com/questions/2513383/access-current-user-in-model) – Brad Werth Jun 30 '15 at 21:21
  • I wish i could compare these two situations and find a solution but im really struggling. Im trying to do this for a completely different situation. Im new to rails, so im sure you are right. Could you perhaps explain how i would go about applying that post to this case. But thank you for you time. – Morphasis Jun 30 '15 at 21:35

1 Answers1

0

Ideally you wouldn't store the email in the order, but if you must you can do it with a simple assignment

    @order = current_user.orders.new(order_params)
    @order.email = current_user.email
    if @order.save

Note that you have two @order =... lines. That doesn't work, the second completely replaces the first. My suggestion above both assigns the current_user.id to the @order and also the form parameters.

If you chose not to store the email in the order record, then you could retrieve the email for an order with my_order.user.email or alternatively...

class Order < ActiveRecord::Base
  belongs_to :user
  delegate :email, to: :user
  ...
end.

Which would let you do...

my_order.email

...without needing the attribute in the Order model.

..

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
  • Thank you for your imput, i really do appreciate your help and im sure its me being dumb but. When i replace this in my controller. It still does not save the email at all. Console Order.last Order Load (11.4ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT 1 => # What am i doing wrong ? – Morphasis Jun 30 '15 at 22:01
  • If you need any more data to help please let me know its something that has been a problem for days im just trying to solve this. Im learning rails. – Morphasis Jun 30 '15 at 22:02
  • So (before the save) you did `@order.email = current_user.email` and it didn't work? Are you sure the current_user.email isn't nil? – SteveTurczyn Jun 30 '15 at 22:06
  • This is what i have. Sorry for the gyazo link it wont let me post the code ill try to change it afterward. http://i.gyazo.com/3fe5badbdbeb5026863c7e700f6f170f.png – Morphasis Jun 30 '15 at 22:11
  • This is the code but the formatting is messed up: ` def create @order = current_user.orders.new(order_params) @order.email = current_user.email if @order.save redirect_to dconfirmation_path end end` – Morphasis Jun 30 '15 at 22:18
  • And when you do `User.last` (I assume that's the current user?) what email do you see? – SteveTurczyn Jun 30 '15 at 22:22
  • Oh, wow, just noticed... you have `attr_accessor :email` in the Order model. Remove that. It's creating a separate `email` attribute, nothing to do with the database column. Once that's gone, it should be fine. – SteveTurczyn Jun 30 '15 at 22:28
  • Ahh ok so i also removed it from the user. It was left over from a example that was clearly not relevant to this. Thank you very much. Let me know if there is anything i can do to show my appreciation. – Morphasis Jun 30 '15 at 22:31
  • No probs. Pass it forward. :) – SteveTurczyn Jun 30 '15 at 22:34