【问题标题】:Simple_form issuesSimple_form 问题
【发布时间】:2018-10-11 17:37:25
【问题描述】:

我对@9​​87654321@ 的关联有疑问。

这里是“链接”的模型

class Contact < ApplicationRecord
  belongs_to :state
end

class State < ApplicationRecord
  CATEGORIES = [ "not-contacted", "on-going", "called"]
  has_many :contacts
  validates :category, inclusion: { in: CATEGORIES }
end

我还有一个State 种子,具有上述三个状态。

我正在为联系人创建新表单。

控制器:

def new
  @contact = Contact.new
  @states = State.all
end

def create
  @contact = Contact.new(contact_params)
  if @contact.save
    redirect_to contact_path(params[:id])
  else
    render :new
  end
end

查看:

<%= simple_form_for(@contact) do |f| %>
  <%= f.input :last_name, label: 'Nom' %>
  <%= f.input :first_name, label: 'Prénom' %>
  <%= f.input :number, label: 'Téléphone' %>
  <%= f.input :email, label: 'Mail' %>
  <%= f.input :address, label: 'Adresse' %>
  <%= f.association :state, collection: State.order(:category), prompt: "Choisir un état" %>
  <%= f.input :grade, label: 'Note', collection:[1, 2, 3, 4, 5]  %>
  <%= f.submit 'Créer le contact', class:'btn-modifications' %>
<% end %>

但我有一个集合没有“正在进行”、“呼叫”、“未联系”的列表,但我有一个列表:

State:0x00007fcb3afcfca8

如何显示我想要的列表?

【问题讨论】:

  • 试试这个&lt;%= f.association :state, collection: State.order(:category), label_method: :category, value_method: :id,prompt: "Choisir un état" %&gt;
  • @Pavan 谢谢,它有效!

标签: activerecord ruby-on-rails-5 simple-form


【解决方案1】:

你需要把它改成下面

<%= f.association :state, collection: State.order(:category), label_method: :category, value_method: :id,prompt: "Choisir un état" %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多