Ruby on rails 简单表单Gem-如何在关联中显示模型的名称-Rails 4
我是新来的rails&我知道这可能是一个非常简单的问题,但我不确定如何使用“简单表单”关联显示公司名称Ruby on rails 简单表单Gem-如何在关联中显示模型的名称-Rails 4,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我是新来的rails&我知道这可能是一个非常简单的问题,但我不确定如何使用“简单表单”关联显示公司名称 在我的模式中,我有一个表companys,其中列name和content 在我的模式中,我还有一个表users,其中列first name,last name&company\u id:integer 一家公司有很多用户 用户所属公司 在我看来,一切都很完美,除了公司名称的展示 我认为: <h2>Sign up Primary Admin</h2> <%=
- 在我的模式中,我有一个表
,其中列companys
和name
content
- 在我的模式中,我还有一个表
,其中列users
,first name
&last name
company\u id:integer
- 一家
公司
有很多用户
用户
所属公司
<h2>Sign up Primary Admin</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :company, collection: Company.all.order(:name), prompt: "please select your company", label: 'Company' %>
<%= f.input :firstname, required: true, autofocus: true %>
<%= f.input :lastname, required: true, autofocus: true %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
注册主管理员
我得到以下显示:
label_method=>要应用于集合的label方法
检索标签(使用此选项而不是中的text_方法选项
收集(请选择)
您应该将label_方法
定义为name
,以显示公司名称
<%= f.association :company, collection: Company.all.order(:name), prompt: "please select your company", label_method: :name, label: 'Company' %>
SimpleForm对关联标签使用
到
方法,因此您必须为您的公司
模型定义自己的到
方法。
我e:
class公司
class Company < ActiveRecord::Base
def to_s
name
end
end