gpt4 book ai didi

ruby-on-rails - 在controller中取一个param,转换成string类型,保存

转载 作者:行者123 更新时间:2023-12-05 02:20:06 24 4
gpt4 key购买 nike

我有一个 ajax post 来创建一个新的 Meeting。我想在保存之前将字符串 meeting_time 转换为 Controller 中的日期时间格式。

我已经试过了,但它仍然将 meeting_time 保存为一个字符串:

def create

requestor = current_user
@meeting_with_params = meeting_params
@meeting_time = @meeting_with_params[:meeting_time]
@meeting_time = @meeting_time.to_datetime
@meeting_with_params[:meeting_time] = @meeting_time
#@meeting_with_params
@meeting = Meeting.new(@meeting_with_params)

respond_to do |format|
if @meeting.save
format.html { redirect_to home_url, notice: 'Your lex was successfully requested! Click Plan Meeting ' }
format.json { render :show, status: :created, location: @meeting }
else
format.html { render :new }
format.json { render json: @meeting.errors, status: :unprocessable_entity }
end
end
end

传递的参数如下所示:

Started POST "/meetings" for 127.0.0.1 at 2016-10-31 11:45:44 -0400
Processing by MeetingsController#create as */*
Parameters: {"meeting"=>{"requestor_id"=>"1", "requestee_id"=>"2", "meeting_time"=>"2016-10-18 22:10:00", "accepted"=>"false", "status"=>"Active", "location"=>"Black Forest Restaurant", "location_address"=>"733 Fulton St, Brooklyn, NY 11217"}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (1.1ms) INSERT INTO "meetings" ("requestor_id", "requestee_id", "meeting_time", "accepted", "location", "status", "location_address", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["requestor_id", 1], ["requestee_id", 2], ["meeting_time", "2016-10-18 22:10:00.000000"], ["accepted", "f"], ["location", "Black Forest Restaurant"], ["status", "Active"], ["location_address", "733 Fulton St, Brooklyn, NY 11217"], ["created_at", "2016-10-31 15:45:44.557266"], ["updated_at", "2016-10-31 15:45:44.557266"]]

然后在 .js 文件中调用 ajax:

$.ajax({
url: '/meetings',
type: 'POST',
data: {
meeting: {
requestor_id: currentUserId,
requestee_id: requesteeId,
meeting_time: rubyDateTime,
accepted: false,
status: "Active",
location: meetingLocation,
location_address: meetingAddress
}
},
success: function(result) {
createNotification(currentUserId, "Your meeting was successfully requested.");
location.reload();
},
error: function(err) {
console.log(err);
}
})

如何在创建 Controller 中拉出meeting_time,并在保存前将其更改为datetime?

谢谢!!

****更新我知道它没有正确保存,因为我还有一种方法可以将今天的日期与 meeting_time 进行比较,如果已经过去,则进行一些操作。直接保存到数据库时,它会识别已经过去的时间。当通过 POST 保存时,使用完全相同的字符串,它不会因为它比较 2 种不同的类型。对比 Controller :

def requestor_review_meeting
@past_meetings = Meeting.where('meeting_time <= ? AND requestor_id= ? AND status = ? AND requestor_score < ?', DateTime.current, current_user, "Active", 1).order('meeting_time asc');
render json: @past_meetings

end

在控制台中:

 > meeting_time
=> Tue, 18 Oct 2016 22:10:00 UTC +00:00
2.1.2 :012 > meeting_time.class
=> ActiveSupport::TimeWithZone
2.1.2 :013 > meeting_time.is_a?(DateTime)
=> false
2.1.2 :015 > meeting_time.is_a?(Time)
=> true

最佳答案

ActiveRecord 会自动对所有输入进行类型转换,以便它们与数据库架构相匹配。因此,即使允许您将 meeting_time 保存为字符串,它也会在加载时将其转换为日期时间。如果您有兴趣,可以阅读更多 here

关于ruby-on-rails - 在controller中取一个param,转换成string类型,保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40345784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com