0

I've got a hash that I define in my plot controller, under the edit action

@custom_params = { :custom_plot_type => "Line", :x_axis_title => "", :x_low => "", :x_high => "", :y_axis_title => "", :y_low => "", :y_high => "" }

When I visit the edit page, I have a form_with @plot as the model containing the following select box:

= fields_for :custom_params do |c|
    = c.label(':custom_plot_type', "Plot Type")
    = c.select(:custom_plot_type, options_for_select(['Line', 'Scatter', 'Heat Map', 'Column'], @custom_params[:custom_plot_type]))

Rails gives me the following error

undefined method `custom_plot_type' for #< Hash:0x00007ffff8c4b030>

I have followed the answer on this question and I'm pretty sure my definition of the hash is correct. But no matter which way I format it (for example, using "custom_plot_type" => "Line" and @custom_params["custom_plot_type"] instead) I get the same error in the view.

I can also output the element I want in the console within the controller, using

puts @custom_params
puts @custom_params[:custom_plot_type]

This outputs the correct value, but as soon as I try to access it in the view it crashes

Edit: Adding more information about the form to clarify in response to a comment. My form is structured as follows:

= form_with(model: @plot, local: true, method: "patch") do |f|
  = label_tag('plot[name]', "New plot name:")
  = text_field_tag('plot[name]', @plot.name)
  %br
  = label_tag('plot[description]', "Description:")
  = text_field_tag('plot[description]', @plot.description)
  %br
  %br
  = fields_for :custom_params do |c|
    = c.label(':custom_plot_type', "Plot Type")
    = c.select(:custom_plot_type, options_for_select(['Line', 'Scatter', 'Heat Map', 'Column'], @custom_params[:custom_plot_type]))

The thinking behind this was to have the a form that has fields that are associated with the Plot model, and also to have a subform for "custom params" that are not associated with the model itself but will be saved to a file. So the form does have a model instance associated with it, but the select tag in question does not.

twigonometry
  • 166
  • 1
  • 9
  • 1
    https://github.com/rails/rails/blob/46a22ceaff1c8dc8e5a8f5acf6bb9865e46b7768/actionview/lib/action_view/helpers/form_helper.rb The comments in the file say that "the f yielded to the block (in your case 'c') is a FormBuilder object. You don't have an instance variable of a model, you've created an arbitrary Hash. I haven't tested this directly so I'm not answering the question - but essentially it doesn't look like you can use an arbitrary Hash with form_for / fields_for in a view. Rails wants an instance of a model that has getters and setters. – tgmerritt May 13 '20 at 17:21
  • Thank you, I have edited to clarify the structure of the form and I will post an answer – twigonometry May 13 '20 at 18:01

1 Answers1

0

I edited the form to use a select_tag instead of a c.select element, as follows:

= fields_for :custom_params do |c|
    = label_tag('custom_params[:custom_plot_type]', "Plot Type")
    = select_tag('custom_params[:custom_plot_type]', options_for_select(['Line', 'Scatter', 'Heat Map', 'Column'], @custom_params[:custom_plot_type]))

I still use the value of c for other form elements later on, but using _tag elements instead of elements related to the form builder allow me to use the arbitrary hash within the select tag - thanks @tgmerritt for the suggestion

twigonometry
  • 166
  • 1
  • 9