gpt4 book ai didi

ruby-on-rails - Rails - Active Record "RuntimeError: can' t 修改卡住字符串“与表单生成器有某种关系?

转载 作者:行者123 更新时间:2023-12-02 02:18:08 25 4
gpt4 key购买 nike

当我提交表单以更新我的模型时,我在浏览器中收到此错误:

ActiveRecord::StatementInvalid in CoachesController#update

RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)

rails 控制台是这样说的:

(0.0ms)  begin transaction
Binary data inserted for `string` type on column `day`
SQL (0.5ms) INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?) [["coach_id", 14], ["created_at", Mon, 27 Feb 2012 21:59:05 UTC +00:00], ["day", "Monday"], ["hour", 20], ["updated_at", Mon, 27 Feb 2012 21:59:05 UTC +00:00]]
RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 25ms

ActiveRecord::StatementInvalid (RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)):
app/models/coach.rb:93:in `block (2 levels) in update_general_availability'
app/models/coach.rb:92:in `each'
app/models/coach.rb:92:in `block in update_general_availability'
app/models/coach.rb:91:in `each'
app/models/coach.rb:91:in `update_general_availability'
app/controllers/coaches_controller.rb:25:in `update'

经过大量试验后,我找到了解决此错误的方法,但不知道为什么我会首先遇到它。

我有两个模型:CoachAvailabilities,具有has_manybelongs_to 关联。这是可用性表的架构:

# Table name: availabilities
# id :integer not null, primary key
# coach_id :integer
# day :string(255)
# hour :integer

它存储一周中的某一天和一天中教练有空的时间。

我在 Coach 模型中编写了两个方法来更轻松地处理教练的每周可用性。他们使用嵌套哈希表,因此您可以查询教练在给定时间是否有空。 (例如:general_availability["Thursday"]["12"] #=> true)

#coach.rb
class Coach < ActiveRecord::Base
...

# Creates a hash table mapping day and hour to true if available then, false otherwise
# Form is general_availability["day"]["hr"]. Per Availability model, "0" = midnight, and
# day of the week is of the form "Monday" or "Tuesday".
def general_availability
h = Hash.new()
%w(Monday Tuesday Wednesday Thursday Friday Saturday Sunday).each { |day| h[day] = Hash.new(false) }

self.availabilities.each do |a|
h[a.day][a.hour.to_s] = true
end

return h
end

# Takes a hash table of the kind returned by general_availability and updates
# this coach's records in the Availabilities table
def update_general_availability(ga_hash_table)
self.availabilities.clear

ga_hash_table.each do |day, hrs|
hrs.each do |hr, val|
self.availabilities.create({day: day, hour: hr.to_i})
end
end
end

这是将教练的每周可用性显示为表格的部分。每个天/小时单元格都是一个复选框,教练可以选中或取消选中以指示他们是否有空。

<!-- availabilities/_scheduler.html.erb -->
<h2>General Availability</h2>
Please check the times below that you would generally be available for a training session.
<table class="table" id="availabilities_table">
<tr>
<th>Time</th>
<% days_of_the_week.each do |day| %>
<th><%= day %></th>
<% end %>
</tr>
<% (6..21).each do |hr| %>
<tr>
<td><%= format_as_time hr %></td>
<% days_of_the_week.each do |day| %>
<% is_checked = @general_availability[day][hr.to_s] %>
<td class="availabilities_cell">
<%= check_box_tag "availability[#{day}][#{hr}]", true, is_checked, :class => 'availabilities_check_box' %>
</td>
<% end %>
</tr>
<% end %>
</table>

这是 Controller :

# coaches_controller.rb
...
def edit
@coach = current_user.coach
@general_availability = @coach.general_availability
end

def update
@coach = Coach.find(params[:id])

@coach.update_attributes(params[:coach])
if @coach.save
@coach.update_general_availability(params[:availability])
redirect_to @coach
end
# ...
end

是线

@coach.update_general_availability(params[:availability])

导致错误。

现在,这是我的问题。 为什么这个 View 会导致上面的错误?

<!-- edit.html.erb version 1 -->
<h1><%= @coach.user.first_name %> </h1>
<%= form_for @coach, :html => { :multipart => true } do |f| %>

<%= f.label :profile_photo %>
<%= f.file_field :profile_photo %>

<div class="field">
<%= f.label :phone_number %>
<%= f.text_field :phone_number %>
</div>

... More Form Fields Here ...

<%= render 'availabilities/scheduler' %>
<%= f.button %>

<% end %>

虽然这个观点没有?

<!-- edit.html.erb version 2 -->
<h1><%= @coach.user.first_name %> </h1>
<%= form_for @coach, :html => { :multipart => true } do |f| %>

<%= f.label :profile_photo %>
<%= f.file_field :profile_photo %>

<div class="field">
<%= f.label :phone_number %>
<%= f.text_field :phone_number %>
</div>

... More Form Fields Here ...

<%= f.button %>

<% end %>

<%= form_for @coach, :class => "form-vertical" do |f| %>
<%= render 'availabilities/scheduler' %>
<%= submit_tag "Update Schedule" %>
<% end %>

请注意,在前者中,部分在表单构建器表单内,而在第二种情况下,部分在下面呈现为它自己的 form_for

从我上面粘贴的日志中跳出来的部分是这一行:

Binary data inserted for `string` type on column `day`

在表单工作时不会出现(例如,在表单的版本 2 中)。这似乎很重要,但我不知道这意味着什么或为什么会发生。

非常感谢!

最佳答案

想通了。 Ruby 哈希表键被卡住。所以我的参数看起来像:

params[:availability][:Thursday][:10] = "true"

当我的 update_general_availability 方法执行此操作时:

self.availabilities.create({day: day, hour: hr.to_i})

:day"Thursday",但 SQLite 适配器将其理解为具有 Encoding::ASCII_8BIT(也称为“二进制”),并尝试进行 编码! 'utf-8' 它。但是,由于它被卡住,因此引发了运行时卡住字符串错误。通过将这些行添加到 update_general_availability 方法解决了这个问题:

day = day.dup
hr = hr.dup

现在,因为它们是重复的而不是哈希键本身,所以它们可以编码为 utf-8。

关于ruby-on-rails - Rails - Active Record "RuntimeError: can' t 修改卡住字符串“与表单生成器有某种关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9474730/

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