Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 如何在另一页上显示错误信息?_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 如何在另一页上显示错误信息?

Ruby on rails 3 如何在另一页上显示错误信息?,ruby-on-rails-3,Ruby On Rails 3,如何在另一页上显示错误? 我在用户文件夹下有一个表单 在另一个地方,my_account文件夹中,我在my_account的索引页中呈现来自user文件夹的表单。这样我就可以重用表单,允许用户更新他们的用户信息 所以,当用户单击update时,将调用UserController中的update方法 我的问题是,如果更新用户对象失败,我如何在我的帐户索引页面上显示错误消息,并且仍然突出显示字段和错误消息?? e、 g电子邮件地址的格式无效 User -- new.html.erb

如何在另一页上显示错误?

我在用户文件夹下有一个表单

在另一个地方,my_account文件夹中,我在my_account的索引页中呈现来自user文件夹的表单。这样我就可以重用表单,允许用户更新他们的用户信息

所以,当用户单击update时,将调用UserController中的update方法

我的问题是,如果更新用户对象失败,我如何在我的帐户索引页面上显示错误消息,并且仍然突出显示字段和错误消息?? e、 g电子邮件地址的格式无效

User
    -- new.html.erb
    -- _form

my_account
    -- index.html.erb
我尝试执行以下操作,但不确定如何在我的帐户页面中打印“错误”:

// try to update user information

// if failed, redirect to my account page
  format.html { redirect_to my_account_path, :error => @user.errors }

我不确定,但可能对你有用

在user_controller的更新方法中,当您收到任何错误时,只需重定向到my_帐户的索引页,并将错误详细信息与之一起传递到索引页上即可。即:

def update
  @user = User.find(params[:id])
  ##  update process and if fails
  redirect_to :action=> 'index', :status=>:unprocessable_entity, :error=>@user.errors
end

您需要在用户对象上设置错误。当在块内调用时,form helper使用此选项显示错误消息

Rails的最佳实践是在这里调用render,而不是redirect,这样我们可以确保传递正确分配了错误的@user对象

要呈现动作,只需调用:

 if !@user.save
   render :action => '../my_account/index'
 end

这应该很容易解决你的问题;只需确保将所有@member变量设置为index.html.erb视图所期望的。

hmm。。。那我怎么才能得到错误消息呢?如果我保留这个“:status=>:unprocessable_entity”,我会有一个空白页,说它正在重定向…上面的代码没有经过测试,你可能需要更新它以适应你的情况。是的。。。我知道。。。我试着。。。。但问题是,我如何读取错误消息?。。我移除了SATU。重定向再次工作。。但不确定如何读取错误消息并将其打印到页面Hi ghayes,我得到了错误缺少模板../my_帐户/索引,其中视图路径中有{:handlers=>[:erb,:rjs,:builder,:rhtml,:rxml],:formats=>[:html],:locale=>[:en,:en]}。我需要在routes配置中执行任何操作吗?将路径更改为“/my_account/index”。。。这对我很有用…太棒了。。