Rails redirect_to another controller method throwing “Template missing”

痴心易碎 提交于 2020-01-04 09:25:27

问题


I gotta be missing something. I have a controller action for short urls:

def shorturl
   redirect_to :action => show, :id => Base58.decode(params[:id]) and return
end

My intention is have this either redirect or load the regular show method of the same controller. At this point, I don't care which way, just want to get it working first.

The problem is it throws a missing template error like its not exiting the shorturl method.

Missing template controller/shorturl with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rhtml, :rxml, :builder, :erb, :rjs]} 

The console shows the query from show is executing so it seems its doing the redirect but still staying inside the method and expecting a view for shorturl instead of the redirected method.


回答1:


open your terminal, and go to project directory and type

   rake routes

Will let you know about the various paths generated from your routes file.

then redirect using

redirect_to xyz_path(:id=> Base58.decode(params[:id]))
return

Edit:--

redirect and render are too different things.

redirect_to(:action=>’my_action’)

will send a 302 redirect request to your browser, the consequence being that any existing variables are lost [2], and the action called ‘my_action’ will be executed.

render(:action=>’my_action’)

will NOT execute the code in the action called ‘my_action’ [1]. It will render the view only. Existing variables will not be lost.



来源:https://stackoverflow.com/questions/5922462/rails-redirect-to-another-controller-method-throwing-template-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!