【问题标题】:Rails redirect issuesRails 重定向问题
【发布时间】:2011-11-18 06:40:32
【问题描述】:

我在我的 rails 应用中设置了一些简单的关联:

resources :properties do
 resources :units
end

更新单元后:

'localhost:3000/properties/2/units/3/edit'

我想被重定向回适当的位置。

点击“更新”后,我被重定向回:

'localhost:3000/units/3',但应该转到'localhost:3000/properties/2/units/3/'

在我的单位控制器中,我有:

if @unit.update_attributes(params[:unit])
    format.html { redirect_to(property_units_path(@property, @unit), :notice => 'Unit was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @unit.errors, :status => :unprocessable_entity }
  end

这在我的 redirect_to 函数中合适吗?我仍在尝试熟悉路线的工作原理,但我觉得这应该可行?

谢谢!

【问题讨论】:

    标签: ruby-on-rails-3 routes redirecttoaction nested-routes


    【解决方案1】:

    你快到了。你应该重定向到:

    format.html { redirect_to(property_unit_path(@property, @unit), :notice => 'Unit was successfully updated.') }
    

    请注意,您必须使用单数版本的路由辅助方法才能使其工作。有关更多详细信息,请查看routing 上的 Rails 指南。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 2013-12-27
      • 1970-01-01
      • 2012-11-26
      • 2011-02-04
      相关资源
      最近更新 更多