u010286013 2008-07-02 10:14
浏览 273
已采纳

render :partial 验证的问题

尝试做做,试着完善 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%>

您的大名

Mail

'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内容:

提示错误

您的大名

Mail

'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!'
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/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%>

{:controller=>'comments',:action=>'create'}, :html => { :id => 'new_comment' } do |f| %> @post.id%>

您的大名

Mail

'comment',:cols=>'60',:rows=>'10',:tabindex=>'4'%>

  • 写回答

1条回答 默认 最新

  • wodeg_2_sdd 2008-07-02 10:29
    关注

    验证会在创建comment的时候自动验证,如果有错会把相关message加入到comment.errors中.与跳到到哪没关系。
    你可以更改跑转到显示blog的页面。
    在那个页面中<%= error_messages_for :comment%>意思就是把comment.errors的错误修信息显示出来。

    一般楼主的这种需录用RJS更加方便。
    在显示blog内容:
    [code="java"]<%= render :partial => "comment/form",:locals => { :button_name => 'Create'}%> [/code]

    _comment.rhtml :
    [code="java"]<% form_remote_for([@post, @comment]) do |f| %> [/code]

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式