Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 简单表单Gem-如何在关联中显示模型的名称-Rails 4_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 简单表单Gem-如何在关联中显示模型的名称-Rails 4

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

我是新来的rails&我知道这可能是一个非常简单的问题,但我不确定如何使用“简单表单”关联显示公司名称

  • 在我的模式中,我有一个表
    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