Rails:重定向到:controller=>'tips',:action=>'show',:id=>@tip.permalink

19

我尝试通过传递控制器、操作和参数来重定向Rails到显示操作。然而,Rails完全忽略了操作的名称!

我得到的是 http://mysite/controllername/paramId

所以我有错误信息...

以下是我使用的操作代码:

def update
    @tip = current_user.tips.find(params[:id])
    @tip.attributes = params[:tip]
    @tip.category_ids = params[:categories]
    @tip.tag_with(params[:tags]) if params[:tags]


    if @tip.save
      flash[:notice] = 'Tip was successfully updated.'
      redirect_to :controller=>'tips', :action => 'show', :id => @tip.permalink
    else
      render :action => 'edit'
    end
  end 
3个回答

35

为什么要与框架斗争呢?

redirect_to @tip

你可以使用 :notice 选项来缩短你的代码。

redirect_to @tip, :notice => 'Message here'

我之前不知道在 redirect_to 中有 :notice 快捷方式。谢谢,这肯定会节省一些时间。那么 flash.now 和 render 有相应的快捷方式吗? - alternative

7
如果这是一个REST资源路由,那么路由实际上是正确的。对于/tips/idGET请求实际上会调用show操作。根据路由的方式,我的猜测是你使用了map.resources进行路由,这是正确的。

-3

这可能是一个临时的“解决办法”。使用渲染,它可以很好地工作...

if @tip.save
  flash[:notice] = 'Tip was successfully updated.'
   render :action => 'show', :id => @tip.permalink
 # redirect_to :controller=>'tips', :action => 'show', :id => @tip.permalink
else
  render :action => 'edit'
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接