在另一个控制器中渲染一个动作 [英] Rendering an action in another controller

查看:41
本文介绍了在另一个控制器中渲染一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Rails 2.3.5)

我有两个脚手架:目录和用户

对于目录显示操作(例如显示操作:\directories\2"),我采用了 User\New 表单并将其设为部分,以便用户可以将用户添加到目录中.我无法弄清楚的是,如果有任何验证错误,我如何在创建操作中返回到\directories\2\show".如果 User.save 成功则返回工作正常,我只是不知道如何格式化 Render 操作以返回目录并在 New User 部分中显示错误消息和字段.

如果保存成功,这可以正常工作,如果出现错误,则使用相同的东西会起作用,除了不会显示 error_messages(我知道错误消息仅假设传递给渲染,而不是重定向,但是当涉及 id 参数时,我无法弄清楚渲染操作所涉及的语法):

format.html { redirect_to directory_path(@user.directory_id) }

用户在目录显示操作中创建由部分调用的操作:定义创建@user = User.new(params[:user])

 respond_to do |format|如果@user.saveflash[:notice] = '用户 ' + @user.name+ ' 已成功创建.format.html { redirect_to directory_path(@user.directory_id) }format.xml { 渲染:xml =>@user, :status =>:created, :location =>@用户}别的# 这里怎么做才能成功返回到'directories\show\(@user.directory_id)'# 以及在此处成功传递 error_messages 的操作结尾结尾结尾

感谢您的帮助 - 希望这是有道理的

解决方案

要从另一个控制器渲染动作,您需要指定要渲染的模板.

<前>渲染:模板 => 'other_controller/view_template_name'

PS:请记住,您必须定义其他控制器操作定义的任何实例变量,这些变量是视图渲染所必需的,因为渲染模板不会在渲染视图之前调用其他控制器的函数.

(Rails 2.3.5)

I have two scaffolds: Directories & Users

For a Directory show action (say Show action: "\directories\2"), I took the User\New form and made it a partial so the user can add users to the Directory. What I can't figure out is how in the create action I can return to "\directories\2\show" if there are any validation errors. Returning if the User.save is successful works fine, I just can't figure out how to format a Render action to return to the directory and display error messages and fields in the New User partial.

This works fine if a save is successful, using the same thing if there is an error will work except error_messages will not be displayed (I know that error messages are only suppose to be passed on a Render, not a redirect, but I can not figure out the syntax involved for a render action when an id parameter is involved):

format.html { redirect_to directory_path(@user.directory_id) }

Users Create Action called by partial in Direcory Show action: def create @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        flash[:notice] = 'User ' + @user.name+ ' was  successfully created.'
        format.html { redirect_to directory_path(@user.directory_id) }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        # what to do here to successfully return to 'directories\show\(@user.directory_id)'
        # and what to do here that successfully passed the error_messages
      end
    end
  end

Thanks for any help - hopefully that makes sense

解决方案

To render an action from another controller you need to specify the template you want to render.

render :template => 'other_controller/view_template_name'

P.S: Keep in mind that you'll have to define any instance variables that the other controller action defines which are necessary for the view to render because rendering a template will not call the other controller's function before rendering the view.

这篇关于在另一个控制器中渲染一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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