如何将验证错误消息传递给不同控制器中的方法? [英] How can I pass validation error messages to a method in a different controller?

查看:29
本文介绍了如何将验证错误消息传递给不同控制器中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚启动了一个具有父模型和子模型(父 has_many :children)的 rails 3 应用程序.

I am just starting a rails 3 app that has a parent model and a child model (parent has_many :children).

我正在尝试进行设置,以便在创建新父项后,用户会被带到该父项 (/parent/id) 的显示操作.在这个视图中,我包含了显示任何孩子的部分和一个创建新孩子的表格.创建新子项后,用户将被重定向到父项的 show 操作,新子项将出现在该处.这一切都按预期工作.

I am trying to set things up so that after creating a new parent the user is taken to the show action of that parent (/parent/id). In this view I have included partials to show any children and a form to create a new child. After creating a new child the user is redirected to the show action for the parent where the new child will appear. This all works as intended.

但是,如果我尝试验证新子表单中的字段,则发生的任何错误消息都不会出现在表单中(视图中的必要行在那里并且是正确的 - 从生成的脚手架代码中剪切和粘贴).有没有办法将孩子的这些错误消息成功传递给父母表演动作?

However, if I try to validate fields in the new child form any error messages that occur are not appearing in the form (the necessary lines in the view are there and are correct - cut and pasted from generated scaffold code). Is there a way of passing these error messages for the child successfully to the parent show action?

以下是相关代码片段;

来自我的父控制器:

def show
  @parent = Parent.find(params[:id])
  @child = @parent.children.new

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @admission }
  end
end

来自我的子控制器:

def create
  @child = Child.new(params[:parent])

  respond_to do |format|
    if @child.save
      format.html { redirect_to(parent_path(params[:child][:parent_id]), :notice => 'Child was successfully created.') }
               #This works as intended
      format.xml  { render :xml => @child, :status => :created, :location => @child }
    else
      format.html { redirect_to parent_path(params[:child][:patient_id]) }
               #This redirects where I want it to go when validation fails but error messages are lost
      format.xml  { render :xml => @child.errors, :status => :unprocessable_entity }
    end
  end
end

推荐答案

好的,我自己解决了.不过感谢 Terw 的回答.除了由于缺少重定向而提供错误的 URL 之外,它还能工作.

Alright, I solved this myself. Thanks for Terw's answer though. It works apart from giving the wrong URL because of the lack of redirect.

这只是通过会话哈希传递错误,然后将它们添加到父控制器中的子模型的问题.

It was simply a matter of passing the errors via the session hash and then adding them to the child model in the parent controller.

我发布代码是因为我在那里找不到任何其他示例.也许有一种更简单的方法,但到目前为止效果很好.如果有人认为我疯了,请解释.

I'm posting the code because I couldn't find any other examples of this out there. Perhaps there's a simpler way but this works perfectly so far. If anyone thinks I'm crazy then please explain.

在我的子控制器中

def create
  @parent = Parent.find(params[:child][:parent_id])
  @child = @parent.child.build(params[:child])

  respond_to do |format|
    if @child.save
      format.html { redirect_to(parent_path(params[:admission][:parent_id]), :notice => 'Child was successfully created.') }
      format.xml  { render :xml => @child, :status => :created, :location => @child }
    else
      if @child.errors.any?
        session[:child_errors] = @child.errors
      end
      format.html { redirect_to(parent_path(params[:child][:parent_id])) }
      format.xml  { render :xml => @child.errors, :status => :unprocessable_entity }
    end
  end
end

在我的父控制器中

def show
  @parent = Parent.find(params[:id])
  @child = @parent.children.new
  if session[:child_errors]
    session[:child_errors].each {|error, error_message| @child.errors.add error, error_message}
    session.delete :child_errors
  end
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @parent }
  end
end

这篇关于如何将验证错误消息传递给不同控制器中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆