Ryan Bates의 우수한 RailsCast # 258을 기반으로하는 문제가 있습니다.레일 스케이프 258 토큰 필드 - through 테이블 - 레일스 방법
이.field
= f.label :skill_tokens, "Skills"
= f.text_field :skill_tokens, data: {load: @user.skills}
그래서 사용자가 많은 기술이 기능을 통해 할당받을 수 있습니다
class User < ActiveRecord::Base
has_many :capabilities,
:dependent => :destroy
has_many :skills, :through => :capabilities,
:uniq => true
has_many :raters,
:through => :capabilities,
:foreign_key => :rater_id,
:uniq => true
attr_accessible :name, :skill_tokens
attr_reader :skill_tokens
def skill_tokens=(tokens)
self.skill_ids = Skill.ids_from_tokens(tokens)
end
end
class Capability < ActiveRecord::Base
belongs_to :user
belongs_to :rater, class_name: "User"
belongs_to :skill
validates_uniqueness_of :rater_id, :scope => [:user_id, :skill_id]
end
class Skill < ActiveRecord::Base
has_many :capabilities
has_many :users, :through => :capabilities,
:uniq => true
has_many :raters, :through => :capabilities,
:foreign_key => :rater_id
end
형태가 식별자로 전달하는 기술 토큰에 대한 일반 텍스트 필드가 포함되어 다음과 같이
상황입니다 . 기술을 배정하는 동안 능력 계급에서 평가자가 추적되어야합니다.
Ryans 사용 jquery TokenInput 예제 사용자가 tokenInput 텍스트 필드를 사용하여 스킬을 할당 (및 생성) 할 수있는 적절한 양식을 만들었습니다.
문제는 이제 데이터를 처리하고 연결이 저장되기 전에 평가자를 설정하는 데 있습니다. 내가 추가 평가자를 설정하려면,
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
분명히 : 컨트롤러의 동작은 매우 간단하므로 일부 루비의 마법을 통해
, 사용자 모델에 대한 self.skill_ids 협회의 모델 생성에 사용되는 ID를 설정합니다 능력 모델에서 update_attributes를 사용하면 쉽게 작동하지 않습니다.이렇게 "레일 방식"으로 어떻게하면 좋을까요? 아름답고 읽기 쉬운 코드를 작성하려면 어떻게해야합니까? 어떤 도움도 대단히 감사하겠습니다!