Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect_to with string path

I'm trying to redirect to a location based on a param on a submitted form. If params[:route] = group , I want to redirect to groups_path. I tried the following method to redirect but obviously enough the groups_path is a variable and not a string. How can I redirect based off the param?

redirect_to "#{params[:route]}s_path"

Edit: realised I can redirect to the actual path but this doesn't seem like a very rails way of doing it.

redirect_to "/#{params[:route]}s"
like image 690
BookOfGreg Avatar asked Mar 24 '12 16:03

BookOfGreg


2 Answers

redirect_to send("#{params[:route].pluralize}_path")

But I'd rather write a wrapper-helper returning appropriate url helper based on the params[:route] value. params[:route] could potentially have any value and you may want to rescue in these cases.

like image 63
jibiel Avatar answered Nov 02 '22 09:11

jibiel


send can calls private method, therefore better is public_send

and instead "#{params[:route]}s" use "#{params[:route].pluralize}

redirect_to public_send("#{params[:route].pluralize}_path")
like image 40
Michal Macejko Avatar answered Nov 02 '22 11:11

Michal Macejko