我有一个名为 content
的 postgres 列,它是一个数组。
但是当尝试以某种形式使用它时,我得到:
无法将动作控制器::P ARMETER 转换为文本
尽管输出看起来相当不错:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"NkK4BggxknfEn0A8shTs06xmesERaZdYtZdl9oEEUTk=",
"notification_template"=>{"content"=>{"0"=>"Join us {{event_time}} {{{twitter_name}}} to win Big! hint: {{{question}}} #quiz {{location_tags}} {{url}} sdfsdfsdf"}},
"commit"=>"Update Notification template",
"id"=>"25"}
强参数
params.require(:notification_template).permit(:name, :content => {})
路线
resources :notification_templates do
get 'edit/:id', to: 'notification_templates#edit_content', as: 'edit_content'
end
控制器
def edit_content
@notification_template = NotificationTemplate.find(params[:notification_template_id])
end
def update
if @notification_template.update(notification_template_params)
redirect_to admin_notification_template_path(@notification_template), notice: 'Social message was successfully updated.'
else
render action: 'edit'
end
end
我的表格
URL 如下所示:/notification_templates/25/edit_content/7 # 自定义操作,但使用正常更新
<%= simple_form_for([:admin, @notification_template]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.simple_fields_for :content do |fields| %>
<%= fields.input params[:id], input_html: { value: @notification_template.content[params[:id].to_i] } %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
数据库列
add_column :notification_templates, :content, :text, array: true, default: []
最后,我不确定添加它的约定。 以上工作正常,但我也注意到其他可能性,例如
add_column :notification_templates, :content, :text, array: true, default: []
add_column :notification_templates, :content, :sting, array: true, default: []
add_column :notification_templates, :content, :text, array: true, default: {}
我选择第一个是基于字符串不允许我最终可能需要的那么多字符,并且文本更方便。 也是[]
与{}
或'{}'
的默认值
但在邮政中是看到content text[] DEFAULT '{}'::text[]
.log
Started PATCH "/admin/notification_templates/25" for 127.0.0.1 at 2014-11-28 14:25:43 +0100
Processing by Admin::NotificationTemplatesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NkK4BggxknfEn0A8shTs06xmesERaZdYtZdl9oEEUTk=", "notification_template"=>{"content"=>{"4"=>"{{{question}}} Study up and stop by {{{twitter_name}}} {{event_time}} for a #quiz {{location_tags}} {{url}} sdfsdfsdf"}}, "commit"=>"Update Notification template", "id"=>"25"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
NotificationTemplate Load (0.5ms) SELECT "notification_templates".* FROM "notification_templates" WHERE "notification_templates"."id" = $1 LIMIT 1 [["id", 25]]
(0.3ms) BEGIN
(0.3ms) ROLLBACK
Completed 500 Internal Server Error in 54ms
Reporting exception: can't cast ActionController::Parameters to text
TypeError (can't cast ActionController::Parameters to text):
app/controllers/admin/notification_templates_controller.rb:40:in `update'
Rendered /Users/holden/.rvm/gems/ruby-2.0.0-p481@questionone-2.0/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
Rendered /Users/holden/.rvm/gems/ruby-2.0.0-p481@questionone-2.0/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered /Users/holden/.rvm/gems/ruby-2.0.0-p481@questionone-2.0/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /Users/holden/.rvm/gems/ruby-2.0.0-p481@questionone-2.0/gems/actionpack-4.1.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (27.5ms)
更新
我还观察到更新数组类型字段在控制台中无法按预期工作。
例如,如果我尝试更新数组的成员,something = record.content[2] = 'blah'
它似乎有效。但是当我保存记录时,它不会更新它。
是的,Rails postgres Arrays仍然有点不稳定。 Hstore稍微容易一些。
通过虚拟属性并明确地做您想做的事情,而不是通过表单依赖标准 rails 行为,您可能会得到更好的服务。
例如。
def content_member=(member)
unless member.blank?
self.content_will_change!
self.content[member.keys.first.to_i] = member.values.first
end
end
您还需要让 rails 知道您是否要更新数组的成员,这就是它在控制台中不起作用的原因。
这里有一个完整的解释:
Rails 4 Postgresql 数组数据类型:更新值