I’m trying to build a rails form that will pass a local variable back to a controller, using a collection_select call, and am generating errors that I can’t for the life of me work out.
The form is in the Network#new view. The collection_select should create a drop down menu of existing superusers, so the new network can be assigned a superuser (via a superuser_id) on creation. The Network#create method knows how to deal with the superuser_id. So my form looks like this:
<%= form_for(@network) do |f| %>
<%=render(partial: 'edit_form', locals: {f: f} ) %>
<% fields_for 'superuser_id', url: {action: :create} do |su| %>
<div>
<%= su.label 'Network Superuser' %>
<%= su.collection_select( 'superuser_id',@superusers, :id, :user_name) %>
</div>
<% end %>
<div>
<%= f.submit %>
</div>
The error I get is:
ActionView::Template::Error (undefined method `superuser_id' for {:url=>{:action=>:create}}:Hash):
6: <% fields_for 'superuser_id', url: {action: :create} do |su| %>
7: <div>
8: <%= su.label 'Network Superuser' %>
9: <%= su.collection_select( 'superuser_id', @superusers, :id, :user_name) %>
app/views/networks/new.html.erb:9:in `block (2 levels) in _app_views_networks_new_html_erb__687644901_79475060'
Any ideas where I am going wrong?
Thanks
Steve
fields_foris designed to handle a sub form of a relationship, and so it is usually used likefields_for :supervisorwithout the _id. However, it looks like this is a:belongs_torelationship, andfields_foris completely unneeded. You aren’t trying to create a form to edit the attributes of the supersuser, you are only assigning an existing id to the network object.Try:
<%= f.collection_select( 'superuser_id',@superusers, :id, :user_name) %>without the fields_for block