2017-09-25 54 views
0

我有一个名为booking的模型,我使用活动管理插件作为资源管理。我在后台有一个名为booking的表单,我需要将当前登录用户(插入表单数据)的IP地址和用户名插入数据库,而不在后端设置任何输入字段。请建议。我使用mysql作为数据库平台。如何在活动管理员中插入IP地址?

这里是我的控制器语法

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 

我试过很多,但仍然没有工作。

回答

1

如果您使用设计进行身份验证,则可以使用方法current_user,请参阅文档在这里:https://github.com/plataformatec/devise#controller-filters-and-helpers

要添加的IP地址,你有ip法,ActionDispatch::Request的一部分。这里检查文档:http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-ip

所以,如果您的预订模式有两种userip字段/方法,您可以在BookingsController

def create 
booking = Booking.new(booking_params) 
booking.user = current_user 
booking.ip = request.ip 
end 
设置他们在 create行动