tl; dr : 추가 처리를 위해 중첩 모델에서 게시 된 값을 가로채는 것이 가능합니까? before_save
콜백에 사용할 중첩 된 특성에 액세스하려고 생각한 모든 것을 시도했지만, 이는 내 상상력의 한계를 입증하는 것일뿐입니다.추가 처리를 위해 nested_attributes를 가로채는 방법은 무엇입니까?
저는 책이 많은 저자를 가질 수있는 도서관 응용 프로그램을 쓰고 있으며 그 반대의 경우도 마찬가지입니다. 그 비트는 has_many_through
과 accepts_nested_attributes_for
으로 잘 작동합니다. 앱은 모든 책과 저자 정보를 저장합니다. 예외 ... 작성하려고하는 작성자가 있는지 확인하기 위해 작성자 또는 도서 모델에 before_save
코드를 작성할 수 없습니다. 여기에 내가 이미 가지고있는 내용은 다음과 같습니다
Parameters: {"commit"=>"Create Book",\
"authenticity_token"=>"/K4/xATm7eGq/fOmrQHKyYQSKxL9zlVM8aqZrSbLNC4=",\
"utf8"=>"✓", "book"{"title"=>"Test", "ISBN"=>"", "genre_id"=>"1", "notes"=>"", \
"authors_attributes"=>{"0"{"author_last"=>"McKinney", "author_first"=>"Jack"}}, \
"publisher"=>"", "pages"=>"", "language_id"=>"1", "location_id"=>"1"}}
을 이렇게 거기 ... 데이터의 :
class Book < ActiveRecord::Base
has_many :authorships
has_many :authors, :through => :authorships
accepts_nested_attributes_for :authors, :authorships
end
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
before_save :determine_distinct_author
def determine_distinct_author
Author.find_or_create_by_author_last(@author)
end
end
## books_controller.rb ##
def new
@book = Book.new
@book.authors.build
respond_to do |format|
format.html
format.xml { render :xml => @book }
end
end
def create
@book = Book.new(params[:book])
@author = @book.authors #I know this line is wrong. This is the problem (I think.)
# more here, obviously, but nothing out of ordinary
end
내가 양식을 게시 할 때, dev에 로그이에 전달합니다. 그러나 그것을 처리하기 위해 어떻게해야합니까? 내가 데이터를 게시 할 때, 여기에 로그입니다 : 내가 중첩 된 속성에이 거기에 뭐가 있는지 주변 검색의 공정한 비트 짓을했는지
Author Load (0.4ms) SELECT `authors`.* FROM `authors` WHERE\
`authors`.`author_last` IS NULL LIMIT 1
, 나는 양식 측면에서 많이 볼 수 있지만 일단 제출되면 자산에 접근하는 방법에 대한 많은 정보를 제공합니다.
실제로 작동하는 솔루션이 실제로 작동하는 이유에 대한 설명도 감사하겠습니다. 감사.
jquery 플러그인에 대한 직접 링크 : [여기] (http://loopj.com/jquery-tokeninput/)에서 직접 건너 뜁니다. – nomadkbro
다행 인 해결책을 찾았 기 때문에 다행이지만'accepts_nested_attributes'에 대한 귀하의 평가에 완전히 동의하지 않습니다. 일반적으로 양식은 올바르게 작성해야 컨트롤러에서 처리 할 필요가 없습니다. 단지 저장하십시오. 그럼에도 불구하고 플러그인은 멋지게 보이고, 나는 그것을 조사 할 것이다 :) – nathanvda