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.
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.
..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.