여러 모델 (예 : A, B)이 다형성으로 설명 모델과 연결되어있는 앱을 만들었습니다. 컨트롤러와 연관된 페이지를 볼 때 액션을 표시하면 A 객체와 관련된 주석이 새 객체를 생성하는 양식과 같이 표시됩니다. 이 모든 것은 작동하며 레일 웹 사이트에 게시 된 Ryan Bates의 15 분 블로그와 비슷합니다. 그러나 사용자가 빈 주석을 제출하지 않도록하기 위해 유효성 검사를 추가하면 렌더링 방법이 확실하지 않습니다. 여기 내 댓글 컨트롤러에있는 내용은 다음과 같습니다 (HAML 사용)의 의견과 양식다형성 설명, 주석 유효성 검사가 실패 할 때 렌더링하는 방법?
before_filter :load_resources, :only => [:create]
def create
if @comment.save
redirect_to @back
else
render @action
end
end
private
def load_resources
@comment = Comment.new(params[:comment])
case @comment.commentable_type
when 'A'
@a = A.find(params[:a_id]
@comments = @a.comments
@back = a_url(@comment.commentable_id)
@resource = @a
@action = 'as/show'
when 'B'
...
end
end
보기 부분 : 내가 알아낼 수있는 것
=render :partial => 'comments/comment', :collection => @comments
%h3 Leave a comment:
-form_for [@resource, Comment.new] do |f|
=f.error_messages
=f.hidden_field :commentable_type, :value => params[:controller].singularize.titleize
=f.hidden_field :commentable_id, :value => params[:id]
=f.hidden_field :editor_id, :value => @current_user.id
=f.hidden_field :creator_id, :value => @current_user.id
%fieldset
=f.label :subject, 'Subject', :class => 'block'
=f.text_field :subject, :class => 'block'
=f.label :text, 'Comment', :class => 'block'
=f.text_area :text, :class => 'block'
.clear_thick
=f.submit 'Submit', :id => 'submit'
는 유효성 검사 오류를 처리하는 방법입니다. 검증 에러가 발생하면 f.error_messages를 트리거하지 않는 것처럼 보입니다. 게다가 render가 실행될 때 사용자는 a/2/comments를 렌더링 할 때 다음 URL을 가진 페이지로 이동합니다.
최신 해결 방법 : 컨트롤러의 show 액션에서 다음
def create
subject = ""
if [email protected]
subject = "?subject=#{@comment.subject}"
end
redirect_to @back + subject
end
: 그것은 종류의 어려운 포장의
아주 좋은 해결책.호기심에서 직접 작성자와 editor_id를 양식에 포함시키지 않는 이유는 무엇입니까? 그것은 컨트롤러의 복잡성을 줄이는 쉬운 방법이라고 생각합니다. 그 말로는 컨트롤러가 컨트롤러보다 깨끗해야합니까? – LDK
LDK, 일반적으로 사용자 입력을 신뢰해서는 안됩니다. 숨겨진 필드가있는 경우에도 악의있는 사용자는 원하는 ID를 양식에 삽입 한 다음이를 전달하여 다른 사용자로 가장 할 수 있습니다. 해당 옵션을 삭제하면 앱의 보안이 강화됩니다. – Carlos