Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get controller and action information from url in rails

How can I get information about controller and action names for any (not only current) url, if there is a route rule for this?

For example:

function_i_need('/pages/5') 

returns

{:controller => 'page', :action => 'show', :id => 5}

UPD: Answer found Rails parse url to hash(Routes)

like image 951
lobanovadik Avatar asked Aug 27 '12 13:08

lobanovadik


People also ask

What does Before_action do in Rails?

When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.

What is Before_filter in Ruby on Rails?

Rails before filters are executed before the code in action controller is executed. The before filters are defined at the top of a controller class that calls them. To set it up, you need to call before_filter method.

What decides which controller receives which requests in Rails?

Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.


1 Answers

you can do this:

Rails.application.routes.recognize_path "/pages/5"
like image 62
Alexander Giraldo Avatar answered Oct 13 '22 00:10

Alexander Giraldo