【问题标题】:Rails 3 - Redirect_to / Render another controllerRails 3 - Redirect_to / 渲染另一个控制器
【发布时间】:2011-08-23 20:25:32
【问题描述】:

我有 2 个控制器:ProjectsUsers。两种模型完全没有关系。

当我创建一个新的Project 时,我想在保存新的project 后重定向到new User path,但我所有的尝试都会出现错误,比如缺少模板或类似的东西。

我怎样才能让它工作?

已编辑

我在Projects控制器中的创建方法:

定义创建

@project = Project.new(params[:project])

respond_to do |format|
  if @project.save     
    format.html { render (new_user_registration_path) }

  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
  end
end

结束

【问题讨论】:

  • 您可以发布您尝试使用的代码吗?
  • 将堆栈跟踪添加到您的问题中

标签: ruby-on-rails controller ruby-on-rails-3.1


【解决方案1】:

你不想渲染new_user_registration_path,你想redirect_to new_user_registration_path

【讨论】:

    【解决方案2】:

    您必须使用 redirect_to 来代替渲染:

     redirect_to new_user_registration_path
    
     respond_to do |format|
       if @project.save     
         format.html { redirect_to new_user_registration_path }
       else
         format.html { render :action => "new" }
         format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
       end
     end
    

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 2014-05-28
      • 1970-01-01
      • 2012-03-07
      • 2016-02-03
      • 1970-01-01
      • 2015-10-25
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多