加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用js form rails呈现错误消息

发布时间:2020-12-16 19:15:29 所属栏目:百科 来源:网络整理
导读:我将表单更改为远程,并且表单现在可以正常工作,如果出现错误,则不再显示错误消息. %= render 'shared/error_messages' % 是否有良好的穿着来重新显示消息? 下面是我的控制器…… 谢谢. respond_to do |format| if @post.save format.js { render :js = "win
我将表单更改为远程,并且表单现在可以正常工作,如果出现错误,则不再显示错误消息.
<%= render 'shared/error_messages' %>

是否有良好的穿着来重新显示消息?

下面是我的控制器……

谢谢.

respond_to do |format|
  if @post.save
    format.js { render :js => "window.location = '#{edit_post_path @post}'" }
    format.html { redirect_to [:edit,@post] }
  else
    format.js { render :js => @post.errors }
    format.html { redirect_to '/',:error => "Could not save comment" }      
  end
end

解决方法

respond_to do |format|
  if @post.save
    format.js { render :js => "window.location = '#{edit_post_path @post}'" }
    format.html { redirect_to [:edit,@post] }
  else
    format.js { }
    format.html { redirect_to '/',:error => "Could not save comment" }      
  end
end

# update.js.erb

$(document).find("form").prepend('<%= escape_javascript(render("shared/error_messages",:formats => [:html])) %>');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读