未定义的方法字符串化键

Undefined method stringify keys

我知道之前有人问过这个问题,通常它与控制器中的更新方法有关。我已经经历过这个,它似乎不是我的问题所在。我认为我的与视图文件和带有提交标记的行有关。我已经尝试了一些事情,但一切都给我关于错误数量参数的错误。任何建议都会很棒!

查看:

= form_for [:admin, @diagnostic], url: admin_diagnostic_path(@diagnostic), html: {method: :put} do |f|
  .field
    = label_tag :notes
    = f.text_field "notes"
    = submit_tag t('.Save'), @diagnostic.data["Save"]

  .field
    = link_to 'show', admin_diagnostics_path

控制器:

def update
  @diagnostic = DiagnosticInfo.find(params[:id])
  if @diagnostic.update_attributes(params[:diagnostic_info])
    redirect_to admin_diagnostic_path, notice: 'Successfully updated.'
  else
    render action: "edit"
  end
end

测试:

it "updates a diagnostic report" do 
  diagnostic_info = FG.create(:diagnostic_info)
  diagnostic_info.save!
  visit edit_admin_diagnostic_path(diagnostic_info)
  fill_in 'diagnostic_info_notes',    with: "test notes"
  click_button "Save"
  page.should have_content("Saved")
  page.should have_content("test notes")
end

你的问题确实是提交标签。 应该这样使用:

submit_tag("display value", html_options)

其中 html_options 是包含以下内容的哈希:

{ class: 'my_class', disabled: true }

所以改变你的 submit_tag 它会起作用的。 您的视图应如下所示:

= form_for [:admin, @diagnostic], url: admin_diagnostic_path(@diagnostic), html: {method: :put} do |f|
  .field
    = label_tag :notes
    = f.text_field "notes"
    = submit_tag @diagnostic.data["Save"], class: "save"

.field
    = link_to 'show', admin_diagnostics_path

编辑: 我认为您现在甚至可以在 Rails 中缩短视图:

= form_for [:admin, @diagnostic] do |f|
  .field
    = f.label{ for: :notes }
    = f.text_field "notes"
    = f.submit @diagnostic.data["Save"], class: "save"
  .field
    = link_to 'show', admin_diagnostics_path

原因是,如果您坚持标准的 CRUD 操作和命名约定,form_for 会自动生成正确的路径和方法

编辑结束

不确定您的@diagnostic 变量中有什么,但我想您希望按钮显示其中的内容?