道喜技术日记 .^. 天天红玉世界

Rails Ruby MacOSX 。。。创新来自于刻苦的实践和勤奋的思考... www.hhtong.com


Rails技巧系列:代码redirect_to(@object)够用了吗

Gudao Luo2009-01-17 at 11:56发表的

代码redirect_to(@object)够用了吗
图片页面HTML代码简化清单
<div id="tabs">
<ul>
<li><a href="#tabs-1">公司</a></li>
<li><a href="#tabs-2">员工</a></li>
<li><a href="#tabs-3">集装箱</a></li>
</ul>
<div id="tabs-1">Company</div>
<div id="tabs-2">Employee</div>
<div id="tabs-3">Container</div>
</div>
  • 说明
    • 代码(一)是通过Rails命令scaffold自动生成代码,其中有一行代码“redirect_to @container”。这一行代码返回到页面,而不能够返回到该页面的某一个标签位置,如上图所示类似页面。如何办?
    • 代码(二)中代码“redirect_to container_path(@container, :anchor => “tabs-3”) ”就是解决这个问题的办法。
代码(一)
  # POST /containers
# POST /containers.xml
def create
@container = Container.new(params[:container]) respond_to do |format|
if @container.save
flash[:notice] = 'Container was successfully created.'
format.html { redirect_to @container) }
format.xml { render :xml => @container, :status => :created, :location => @container }
else
format.html { render :action => "new" }
format.xml { render :xml => @container.errors, :status => :unprocessable_entity }
end
end
end
代码(二)
  # POST /containers
# POST /containers.xml
def create
@container = Container.new(params[:container]) respond_to do |format|
if @container.save
flash[:notice] = 'Container was successfully created.'
format.html { redirect_to container_path(@container, :anchor => "tabs-3") }
format.xml { render :xml => @container, :status => :created, :location => @container }
else
format.html { render :action => "new" }
format.xml { render :xml => @container.errors, :status => :unprocessable_entity }
end
end
end
Hierarchy: previous, next