Ruby on rails 如何通过关联的用户id限制下拉列表的内容?

Ruby on rails 如何通过关联的用户id限制下拉列表的内容?,ruby-on-rails,model-view-controller,view,ruby-on-rails-4,Ruby On Rails,Model View Controller,View,Ruby On Rails 4,在我的rails视图中,我有一个带有下拉列表的表单,如下所示: <%= simple_form_for(@appointment) do |f| %> <%= f.error_notification %> <div class="form-inputs"> <%= f.association :client, label_method: lambda { |c| "#{c.first_name} #{c.last_name}" } %

在我的rails视图中,我有一个带有下拉列表的表单,如下所示:

<%= simple_form_for(@appointment) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.association :client, label_method: lambda { |c| "#{c.first_name} #{c.last_name}" } %>
    <%= f.input :occured_on %>
    <%= f.input :start %>
    <%= f.input :end %>
    <%= f.input :copay_received %>
  </div>

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

我想将下拉列表的内容仅限于与当前_user.id关联的客户端

我如何做到这一点


如果您需要查看我的控制器或其他东西,请告诉我,但它不起任何作用。我打赌您可以猜出它的内容。

只需添加collection参数:

<%= f.association :client, label_method: lambda { |c| "#{c.first_name} #{c.last_name}" }, collection: Client.where(user_id: current_user.id) %>

备选方案:

# this implies you have declared: user has_many :clients
<%= f.association :client, collection: current_user.clients, label_method: lambda{|c| "..."} %>
#这意味着您已声明:user有多个:客户端

您可以使用collectionsweet进行限制+1对于不太详细的备选方案:-)