Rails查看:协会:未找到公司

时间:2014-10-14 09:50:11

标签: ruby-on-rails forms associations views

有用户模型和公司模型。

User.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many :companies
end

Company.rb

class Company < ActiveRecord::Base
  has_and_belongs_to_many :users
end

New.html.erb

<%= simple_form_for(@user) do |f| %>

<% if policy_scope(Company).count <= 1 %>
  <%= f.label :company, "Company: "%><br />
  <%= f.text_field :company, :value => policy_scope(Company).first %><br /><br />
  <% else %> 
  <%= f.association :company, collection: policy_scope(Company).all, prompt: "Choose a Company", :required => true %>
  <% end %>

  <%= f.input :username, :required => true %>
  <%= f.input :display_name, :required => true %>

  <div class="form-actions">
    <%= f.button :submit, :class => 'btn-primary' %>
  </div>
<% end %>

f.association未能说:公司未找到。如果是policy_scope(Company).count <= 1,则该条件中的代码可以正常工作。

请说明您是否需要更多代码。谢谢

0 个答案:

没有答案