2
이 문제에 관한 많은 게시물을 읽었지만 결코 작동하지 않습니다.Ruby on Rails - f.error_messages가 보이지 않음
내 모델은 다음과 같습니다
class Announcement < ActiveRecord::Base
validates_presence_of :title, :description
end
내 컨트롤러의 방법 (에만 관련 부분)을 생성은 다음과 같습니다
내 양식은 다음과 같습니다def create
respond_to do |format|
if @announcement.save
flash[:notice] = 'Announcement was successfully created.'
format.html { redirect_to(@announcement) }
format.xml { render :xml => @announcement, :status => :created, :location => @announcement }
else
@announcement = Announcement.new
@provinces = Province.all
@types = AnnouncementType.all
@categories = Tag.find_by_sql 'select * from tags where parent_id=0 order by name asc'
@subcategories= ''
format.html { render :action => "new" } #new_announcement_path
format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
end
end
end
:
<% form_for(@announcement) do |f| %>
<%= error_messages_for 'announcement' %> <!--I've also treid f.error_messages-->
...
내가 뭘 잘못하고 있니?
네가 맞아. 그것이 문제였습니다. 왜 내가 렌더링하기 전에 else 문에 그 줄을 넣었는지 아십니까? 왜냐하면 나는 그 변수들로 초기화되어야하는 콤보 상자를 가지고 있기 때문에 (나는 "새로운"방법으로 그것들을 생성한다) 나는 else 절에 넣지 않으면 예외를 던질 것이다. 어디에서나 반복되는 것을 피하기 위해 어떻게 그 라인을 재사용 할 수 있습니까? 감사! –
컨트롤러에 변수를 설정하고 필요한 경우 함수를 호출하는 전용 함수를 만듭니다. –
고마워요! 이제 작동합니다. –