0

collection_select 呼び出しを使用して、ローカル変数をコントローラーに戻す Rails フォームを作成しようとしていますが、一生解決できないエラーを生成しています。

フォームは Network#new ビューにあります。collection_select は既存のスーパーユーザーのドロップダウン メニューを作成する必要があるため、作成時に新しいネットワークに (superuser_id を介して) スーパーユーザーを割り当てることができます。Network#create メソッドは、superuser_id の処理方法を知っています。したがって、私のフォームは次のようになります。

<%= 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>

私が得るエラーは次のとおりです。

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'

私が間違っているアイデアはありますか?

ありがとう

スティーブ

4

1 に答える 1

1

fields_for関係のサブフォームを処理するように設計されているため、通常fields_for :supervisorは_idなしのように使用されます。ただし、これは:belongs_to関係のようであり、fields_forまったく不要です。スーパーユーザーの属性を編集するためのフォームを作成しようとしているのではなく、既存のIDをネットワークオブジェクトに割り当てるだけです。

試してください:<%= f.collection_select( 'superuser_id',@superusers, :id, :user_name) %>fields_forブロックなし

于 2012-05-31T13:55:01.213 に答える