많은 질문이 있지만 도움이되지는 않습니다. 그리고 예, 나는 this rails cast을 보았다.중첩 모델 유효성 검사 - 오류가 표시되지 않습니다.
나는 많은 책과 같이이있는 저자 :
저자 :
class Book < ActiveRecord::Base
attr_accessible :name, :year
belongs_to :author
validates :name, :year, presence: true
validates :year, numericality: { only_integer: true, less_than_or_equal_to: Time.now.year }
end
내가 저자에 책을 추가하려면 다음 양식을 작성 : 책이
class Author < ActiveRecord::Base
attr_accessible :name
has_many :books, dependent: :destroy
accepts_nested_attributes_for :books, allow_destroy: true
validates :name, presence: true
validates :name, length: { minimum: 3 }
end
저자 : # show :
<%= form_for([@author, @book], html: { class: "well" }) do |f| %>
<% if @book.errors.any? %>
<div class="alert alert-block">
<ul>
<% @author.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
#labels and buttons...
<% end %>
다음 authors_controller 방법으로
... :
def show
@author = Author.find(params[:id])
@book = @author.books.build
end
... 다음과 같은 books_controller 방법 : I 양식이 오류 메시지를 표시하지 않는 이유를 알아낼 수 없습니다
def create
@author = Author.find(params[:author_id])
if @author.books.create(params[:book])
redirect_to author_path(@author)
else
render action: :show
end
end
. 나는 railscasts의 예제를 따라 @ author.books.build 대신에 폼의 인스턴스 변수가 있어야한다고 말했기 때문에 나는 후자를 컨트롤러에두고 @book을 폼에 넣어 뒀다.
도움 주셔서 감사합니다.
이미이 자세한 답변을 주셔서 감사합니다 책
/books/new.html.erb을위한 새로운 형태의 나열했습니다 수 있습니다! 책 컨트롤러에 새로운 방법이 없습니다. 저자가 표시되면 새 책의 양식이 표시됩니다. 또한 books # create 메서드를 제안대로 변경하면 편집 메서드를 찾을 수 없다는 라우팅 오류가 발생합니다. 이 문제를 해결하려면 어떻게해야합니까? – weltschmerz
리디렉션을 new : new로 변경하는 것을 잊었습니다. 표준 RESTful 디자인 (반드시 최고의 디자인은 아님)은 필자가 설명한 것처럼 책을위한 새로운 페이지가있을 것이다. 다른 방식으로 작동 시키려면 잘못된 것이 무엇인지 이해하고 원하는 방식으로 수정하십시오. –