Rails: How to get the controller & method name of Restfull named routes? -
in html view
<% if check_link(dashboard_path) %> <%= link_to "products", dashboard_path, class: controller_name == "dashboard" ? "active" : nil %> <% end %> in check_link helper method:
def check_link(path) controller_name = path.controller method_name = path.action have access verification code ............ end but, getting error below in browser:
undefined method `controller' "/admin/dashboard":string
now, question how find controller , method name "named routes(dashboard_path)". please help me resolve issue.
you can utilize rails.application.routes.recognize_path extracting :controller , :action
def check_link(path) extracted_path = rails.application.routes.recognize_path(path) controller = extracted_path[:controller] action = extracted_path[:action] # rest of codes goes here end example
rails.application.routes.recognize_path('/parties/autocomplete_party_name_last', {:method => :get}) # output {:action=>"autocomplete_party_name_last", :controller=>"parties"} or rails.application.routes.recognize_path('/transcriptions/2/edit') # output => {:action=>"edit", :controller=>"transcriptions", :id=>"2"} if path wrong this:
rails.application.routes.recognize_path('dashboards/index') # output => {:controller=>"pages", :action=>"page_not_found", :url=>"dashboards/index"} hope find useful.
ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-3.1
No comments:
Post a Comment