Ruby on rails 在Postgres中使用Rails阵列
我有一个名为Ruby on rails 在Postgres中使用Rails阵列,ruby-on-rails,postgresql,ruby-on-rails-4,strong-parameters,ruby-on-rails-4.1,Ruby On Rails,Postgresql,Ruby On Rails 4,Strong Parameters,Ruby On Rails 4.1,我有一个名为content的postgres列,它是一个数组 但当我试图以一种形式使用它时,我得到: 无法将ActionController::参数强制转换为文本 尽管输出看起来相当不错: {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"NkK4BggxknfEn0A8shTs06xmesERaZdYtZdl9oEEUTk=", "notification_template"=>{"content"=
content
的postgres列,它是一个数组
但当我试图以一种形式使用它时,我得到:
无法将ActionController::参数强制转换为文本
尽管输出看起来相当不错:
{"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"}
强参数
路线
控制器
我的表格
url看起来像:/notification_templates/25/edit_content/7#自定义操作,但使用普通更新
最后,我不确定加入它的惯例。上述方法效果不错,但我也注意到了其他可能性,例如
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: {}
我之所以选择第一个,是因为字符串不能容纳我最终可能需要的那么多字符,而且文本更方便。还有默认值[]
与{}
或{}
但是在postgres中是seecontent text[]默认值“{}”:text[]
日志
更新
我还注意到,updatearray type字段在控制台中并没有像预期的那样工作
如果我试图更新数组的一个成员,
something=record.content[2]=“blah”
它似乎可以工作。但是当我保存记录时,它不会更新它。是的,Rails postgres数组仍然有点不稳定。Hstore稍微容易一点
通过虚拟属性并明确地做您想要做的事情可能比通过表单依赖标准rails行为更好
例如
如果要更新数组的成员,还需要让rails知道,这就是为什么它不能在控制台中工作
这里有一个完整的解释:
参数。需要(:通知\模板)。允许(:名称,:内容=>{})
?嘿,这似乎有效!使该部分正常工作,谢谢。请指定完整跟踪日志,并在例外行中显示代码:`app/controllers/admin/notification\u templates\u controller.rb`请尝试@notification\u template.update(notification\u template\u params.to\u h)
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
<%= 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: {}
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)
def content_member=(member)
unless member.blank?
self.content_will_change!
self.content[member.keys.first.to_i] = member.values.first
end
end