问题描述
尝试做做,试着完善 Rolling with Rails 2.0 - The First Full Tutorial 这篇文章的code,遇到一个问题,请教下在显示blog内容页面下面采用:<%= render :partial => @comment = Comment.new, :locals => { :button_name => 'Create'}%>局部模板到_comment.rhtml<% form_for([@post, @comment]) do |f| %><%= error_messages_for :comment%><p><%= f.text_field :author%><label for="author"><small>您的大名</small></label></p><p><%= f.text_field :email%><label for="email"><small>Mail</small></label></p><p><%= f.text_area :body,:id=>'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%></p><p><input name="button_name" type="image" src="/images/submit.png" id="submit" tabindex="5" value="SubmitComment" /></p><% end %>comment_controller中:format.html { redirect_to(@post, @comment) }跳转到post show的页面去,这个时候验证怎么办?在model/comment.rb中:validates_presence_of :body,:on => :create,:message =>"请填写内容!" validates_format_of :email,:on => :create,:message => "Email格式不正确!",:with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})Z/i问题补充:_comment.rhtml内容:<div name="form"><% form_remote_for([@post, @comment]) do |f| %> <span id='markerror' style='display:none;color:red;'>提示错误</span><p><%= f.text_field :author%><label for="author"><small>您的大名</small></label></p><p><%= f.text_field :email%> <label for="email"><small>Mail</small></label></p><p><%= f.text_area :body,:id=>'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%></p><p><input name="button_name" type="image" src="/images/submit.png" id="submit" tabindex="5" value="SubmitComment" /></p><% end %></div>=========================================comment_controller的create方法:def create @comment = @post.comments.build(params[:comment]) respond_to do |format|format.js doif @comment.saverender :update do |page|page.insert_html :bottom,'comments',:partial => 'comment',:object => @commentpage['markerror'].hide()endelserender :update do |page|page['markerror'].update @comment.errors.full_messages;endendend end=========================================views/posts/show.html.erb改为:<%= render :partial => @comment = Comment.new%> =========================================现在点击comment提交按钮没有任何反应,还望指点迷津问题补充:========================================================================================================================================================仔细检查改进后,现在出现的情况是后台提示如下,但是页面上却没有ajax提示出来:========================================================================================================================================================ActiveRecord::RecordInvalid (Validation failed: Body 请填写内容!, Email Email格式不正确!): D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/validations.rb:913:in `save_without_dirty!' D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/dirty.rb:83:in `save_without_transactions!' D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:110:in `save!' D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'============================================================================<p id="markerror" style='display:none;color:red;'>提示错误</p><p id="notice" style="color: green"><%= flash[:notice] %></p><h3>发表回复:</h3><%= render :partial => @comment = Comment.new%>============================================================================<div id="new_comment_from"><% form_remote_for :comment, :url=>{:controller=>'comments',:action=>'create'}, :html => { :id => 'new_comment' } do |f| %><%= f.hidden_field :post_id,:value=>@post.id%><p><%= f.text_field :author%><label for="author"><small>您的大名</small></label></p><p><%= f.text_field :email%> <label for="email"><small>Mail</small></label></p><p><%= f.text_area :body,:id=>'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%></p><p><input name="button_name" type="image" src="/images/submit.png" id="submit" tabindex="5" value="SubmitComment" /></p><% end %></div>============================================================================
解决方案
验证会在创建comment的时候自动验证,如果有错会把相关message加入到comment.errors中.与跳到到哪没关系。你可以更改跑转到显示blog的页面。在那个页面中<%= error_messages_for :comment%>意思就是把comment.errors的错误修信息显示出来。一般楼主的这种需录用RJS更加方便。在显示blog内容:<%= render :partial => "comment/form",:locals => { :button_name => 'Create'}%> _comment.rhtml :<% form_remote_for([@post, @comment]) do |f| %> comment_controller:respond_to do |format| format.js do if @comment.save render :update do |page| page.insert_html :bottom,'commentss',:partial => 'comment',:object => @comment end else render :update do |page| page['error'].update @comment.errors.full_messages; end end end end