I have a model called booking and I am using active admin plugin as a resource management. I have a form called booking in the backend in which I need to insert ip address and username of the currently logged in user(who insert the form data) to the database without making any input field in backend. Please suggest. I am using mysql as database platform.
我有一个名为预订的模型,我使用主动管理插件作为资源管理。我在后端有一个名为booking的表单,我需要在后端插入任何输入字段,将当前登录用户的ip地址和用户名(插入表单数据)插入数据库。请建议。我使用mysql作为数据库平台。
here is my controller syntax
这是我的控制器语法
class CustomizedBookingsController < ApplicationController
def create
customized_booking = CustomizedBooking.new(params[:customized_booking])
customized_booking.booked_by = current_admin_user
customized_booking.booking_ip = request.remote_ip
end
end
I tried lots but still not working.
我尝试了很多,但仍然没有工作。
1 个解决方案
#1
1
If you're using Devise for authentication, you can use the method current_user
, see the documentation here: https://github.com/plataformatec/devise#controller-filters-and-helpers
如果您使用Devise进行身份验证,可以使用current_user方法,请参阅此处的文档:https://github.com/plataformatec/devise#controller-filters-and-helpers
To add the IP address, you have the ip
method, part of the ActionDispatch::Request
. Check the documentation here: http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-ip
要添加IP地址,您需要使用ip方法,它是ActionDispatch :: Request的一部分。请查看以下文档:http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-ip
So if your booking model has both user
and ip
fields/methods, you can set them in the create
action in your BookingsController
因此,如果您的预订模型同时具有用户和IP字段/方法,则可以在BookingsController中的create操作中设置它们
def create
booking = Booking.new(booking_params)
booking.user = current_user
booking.ip = request.ip
end
#1
1
If you're using Devise for authentication, you can use the method current_user
, see the documentation here: https://github.com/plataformatec/devise#controller-filters-and-helpers
如果您使用Devise进行身份验证,可以使用current_user方法,请参阅此处的文档:https://github.com/plataformatec/devise#controller-filters-and-helpers
To add the IP address, you have the ip
method, part of the ActionDispatch::Request
. Check the documentation here: http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-ip
要添加IP地址,您需要使用ip方法,它是ActionDispatch :: Request的一部分。请查看以下文档:http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-ip
So if your booking model has both user
and ip
fields/methods, you can set them in the create
action in your BookingsController
因此,如果您的预订模型同时具有用户和IP字段/方法,则可以在BookingsController中的create操作中设置它们
def create
booking = Booking.new(booking_params)
booking.user = current_user
booking.ip = request.ip
end