Ruby-on-rails Rails:redirect_to :controller=>'tips', :action => 'show', :id => @tip.permalink

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3026394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:46:15  来源:igfitidea点击:

Rails: redirect_to :controller=>'tips', :action => 'show', :id => @tip.permalink

ruby-on-railsaction

提问by john

I tried to redirect rails to show action by passing controller, action, and params. However, rails ignores the name of action totally!

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

what I got is http://mysite/controllername/paramId

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

so i have error message....

所以我有错误信息....

here is the action code I used:

这是我使用的操作代码:

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 

采纳答案by john

This might be a temporary "get around". Using render, it works very well....

这可能是暂时的“绕过”。使用渲染,它工作得很好......

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

回答by Samuel

Why fight the framework?

为什么要对抗框架?

redirect_to @tip

And you can shorten your code by using the :notice option.

您可以使用 :notice 选项缩短代码。

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

回答by alternative

If its a REST resource route, the routing is actually correct. A GETrequest on /tips/idactually calls the showaction. Because of the way it is routing, my guess is that you routed it with map.resources, and this is correct.

如果是 REST 资源路由,则路由实际上是正确的。上的GET请求/tips/id实际上调用了该show操作。由于它的路由方式,我的猜测是您使用 路由它map.resources,这是正确的。