尝试做做,试着完善 Rolling with Rails 2.0 - The First Full Tutorial 这篇文章的code,遇到一个问题,请教下
在显示blog内容页面下面采用:
[code="java"]<%= render :partial => @comment = Comment.new,
:locals => { :button_name => 'Create'}%>[/code]
局部模板到_comment.rhtml
[code="java"]
<% form_for([@post, @comment]) do |f| %>
<%= error_messages_for :comment%>
您的大名
'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%>
<% end %>
[/code]
comment_controller中:
[code="java"]
format.html { redirect_to(@post, @comment) }
[/code]
[color=red]跳转到post show的页面去,这个时候验证怎么办?[/color]
在model/comment.rb中:
[code="java"]
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
[/code]
[b]问题补充:[/b]
_comment.rhtml内容:
您的大名
'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%>
comment_controller的create方法:
def create
@comment = @post.comments.build(params[:comment])
respond_to do |format|
format.js do
if @comment.save
render :update do |page|
page.insert_html :bottom,'comments',:partial => 'comment',:object => @comment
page['markerror'].hide()
end
else
render :update do |page|
page['markerror'].update @comment.errors.full_messages;
end
end
end
end
views/posts/show.html.erb改为:
<%= render :partial => @comment = Comment.new%>
现在点击comment提交按钮没有任何反应,还望指点迷津
[b]问题补充:[/b]
仔细检查改进后,现在出现的情况是后台提示如下,但是页面上却没有ajax提示出来:
ActiveRecord::RecordInvalid (Validation failed: Body 请填写内容!, Email Email格
式不正确!):
D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/validati
ons.rb:913:in save_without_dirty!'
save_without_transactions!'
D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/dirty.rb
:83:in
D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transact
ions.rb:110:in `save!'
D:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connecti
on_adapters/abstract/database_statements.rb:66:in `transaction'
提示错误
发表回复:
<%= render :partial => @comment = Comment.new%>
您的大名
'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%>