레일 2.3.11을 사용 중입니다. 내 add_blog.js.rjs이레일에있는 자동 완성에서 사용자의 존재를 확인하는 방법
if @saved.nil?
page.replace_html 'message','<div id="msg" class = "textcolor3">' + @blog.errors.full_messages.join('<br />') + '</div>'
else
page.replace_html 'message','<div id="msg" class = "h3txt textcolor3"> Blog created successfully </div>'
end
파일에서 add_blog 액션 내가
def add_blog
unless params[:blog].nil?
@blog = Blog.new(params[:blog])
@user = User.find_by_login(params["user_login"][:login])
@blog.owner = @user
end
respond_to do |format|
if @blog.save
@saved = true
format.js { render :action => 'add_blog' }
else
@saved = nil
format.js { render :action => 'add_blog' }
end
end
end
이 모델 블로그 blogs_controller에서
<% remote_form_for :blog, :url => {:controller => "blogs", :action =>"add_blog"} do |f|%>
<%= f.text_field :title, :class => "admtxt1 wd1 lfloat" %>
<%= text_field_with_auto_complete "user_login", :login, { :size => 15}, { :url => {:controller => 'users', :action => 'autocomplete_for_blogowners'}, :method => :get, :param_name => 'term', :indicator => "spinner"} %>
<%= submit_tag "Save", :class => "btn-small margintop10" %>
<%end%>
의 형태로 내부에 사용되는 text_field_with_autocomplete 일이 내 블로그 tablehas id, title, owner_id
그리고 내 모델의 유효성은입니다.validates_presence_of : OWNER_ID : 제목
위의 코드는 소유자의 검증과 함께 완벽하게 작동은
빈 야해 그러나 그것은 잘못된 사용자 예를 들어
에 대한 검사를 일부러. 만약 내가 그런 사람이 존재하지 않는 무언가를 입력 오전. 그 경우 상자에 결과가없는 양식을 제출하는 경우, 심지어 tat 시간 나는 소유자가 비워두면 안된다.
이 경우 사용자의 존재를 확인하고 오류 메시지에 추가하는 방법.
우리는 요구 사항에 따라 3 개의 레일을 사용하지 않습니다. – useranon