使用虚拟属性rails simple_form

时间:2014-11-24 19:36:28

标签: ruby-on-rails-4 autocomplete simple-form model-associations virtual-attribute

我正在尝试使用虚拟属性将Simple_forms合并到我的应用中。我正在关注http://railscasts.com/episodes/102-auto-complete-association-revised以获得自动填充功能。当我不使用虚拟属性时,Simple_form有效,但当我使用虚拟属性时,我得到错误"关联:未找到head_coach_name"。

我的团队模型是:

class Team < ActiveRecord::Base
  attr_accessor :head_coach_name
  belongs_to :user
  validates :team_name, presence:   true
  belongs_to :head_coach, class_name: "User", :foreign_key => "head_coach_id"

  def head_coach_name
    user.try(:name)
  end

  def head_coach_name=(name)
    self.user = User.find_by_name(name) if name.present?
  end
end

我的用户模型是:

class User < ActiveRecord::Base
  has_many :teams, :class_name => "::Team", dependent: :destroy
end

我的观点:

                <%= simple_form_for @team, html: {class: 'form-horizontal' }, url: teams_path, method: 'post' do |f| %>     
                <%= f.error_notification %>         
                <%= f.hidden_field :user_id %>                  
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-md-12">                         
                                <%= f.input :team_name, class: "form-control" %>        
                                <%= f.input :year, collection: Date.today.year-90..Date.today.year  %>  
                                <%= f.input :season, as: :select, collection: 
                                    ['Fall',
                                    'Winter',
                                    'Spring',
                                    'Summer'] %>                        
                                <%= f.input :season_type, as: :select, collection:
                                    ['Outdoor',
                                    'Indoor',
                                    'Tournament'] %>
                                <%= f.input :awards %>  
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-md-12">         
                                <%= f.input :club %>        
                                <%= f.input :division %>
                                <%= f.association :head_coach_name %>                       
                                <%= f.input :assistant_coach_id %>          
                                <%= f.input :assistant_coach_two_id %>
                            </div>                                              
                        </div>
                    </div>          
                <%= f.button :submit, label: "Add team", class: "btn btn-large btn-primary col-md-3 pull-right" %>
                <% end %>   

只要我没有虚拟关联,一切都会有效。我可以把:head_coach,它可以使用下拉列表,但我想使用像railscast视频中的自动完成功能。同样在rails控制台中,我可以运行这些命令来显示虚拟属性:

2.1.2 :003 > team.head_coach_name
  User Load (0.6ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 100 LIMIT 1
 => "Miss Daisha Shanahan"

关于如何使用simple_forms使虚拟属性正常工作的任何想法?

1 个答案:

答案 0 :(得分:0)

您正在引用与f.association :head_coach_name不存在的关联。试试这个:

f.association :head_coach, label_method: :head_coach_name

您的代码中还有其他一些奇怪的内容,包括head_coach_name定义,它应该依赖于head_coach而不是用户。同样关注head_coach_name=方法。

实施例。你有:

def head_coach_name
  user.try(:name)
end

好像你应该有:

def head_coach_name
  head_coach.try(:name)
end