我认为代码更明确
class RedirectController < ApplicationController
  def index
    redirect_to :controller => 'posts', :action => 'show', :id => 1
    # it works
  end
end
class RedirectController < ApplicationController
  def index
    render :controller => 'posts', :action => 'show', :id => 1
    # it doesn't work
  end
end
(B)可以在另一个控制器中加载另一个动作吗?(而不只是视图)怎么样?谢谢
(Rails 2.3.5)
我有两个脚手架:目录和用户
对于目录显示操作(比如显示操作:"\ directories\2"),我使用了User\New表单并将其设置为部分,以便用户可以将用户添加到目录.我无法弄清楚的是,如果有任何验证错误,我可以在创建操作中返回"\ directories\2\show".返回如果User.save成功工作正常,我只是无法弄清楚如何格式化Render操作以返回到目录并在New User partial中显示错误消息和字段.
如果保存成功,这可以正常工作,如果有错误将使用相同的东西将工作,但不会显示error_messages(我知道错误消息只是假设在渲染上传递,而不是重定向,但我不能当涉及到id参数时,弄清楚渲染动作所涉及的语法):
format.html { redirect_to directory_path(@user.directory_id) }
用户在Direcory Show动作中创建由partial调用的动作: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
感谢您的帮助 …
所以我试图从我的表单中获取错误,这些错误在我的 root_path 中呈现为部分内容。在我尝试发布它并失败(或成功)之后,我想重定向回 root_path。但是,redirect_to 决定不保存任何验证信息。
想知道如何做到这一点。
class PostsController < ApplicationController
  def new
    @post = Post.new
  end
  def create
    @nom = current_user.noms.build(params[:nom])
    if @nom.save
      flash[:success] = "Nom created!"
      redirect_to root_path
    else
      flash[:error] = @nom.errors
      redirect_to root_path
  end
在我的主页/索引中,我呈现了帖子形式的部分内容。
= form_for [current_user, @post] do |f|
  = f.text_field :name
  = f.select :category
  = f.text_area :description
  = f.submit "Post", class: "btn btn-primary"
  - @post.errors.full_messages.each do |msg|
    %p
      = msg
在重定向到 root_path 后,它应该将错误保留在表单的底部。
我还想保留验证失败后的信息。