Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Rails views to return its associated action names?

I have this very simple controller for managing static pages within my Rails application:

class PagesController < ApplicationController

  def home
  end

  def features
  end

  def pricing
  end

end

How can I get a view template to return its own name, so I can do something like this:

# pricing.html.erb

<h1><%= my_own_name.capitalize %></h1>

# --> "Pricing"

Thanks for any help.

like image 550
Tintin81 Avatar asked Apr 28 '14 16:04

Tintin81


2 Answers

4.3 Routing Parameters

The params hash will always contain the :controller and :action keys, but you should use the methods controller_name and action_name instead to access these values.

<h1><%= action_name.capitalize %></h1>
like image 160
Arup Rakshit Avatar answered Sep 19 '22 16:09

Arup Rakshit


So:

  class PagesController < ApplicationController

    def pricing
      @action = params[:action]
    end

  end


  # pricing.html.erb
 <h1><%= @action.capitalize %></h1>
like image 34
zishe Avatar answered Sep 17 '22 16:09

zishe