3

I am trying to dynamically create methods for semi static pages. However, the code below does not seem to work. I get a wrong number of arguments (0 for 1) error.

class PagesController < ApplicationController

  pages = %w[ page1 page2 page3 ]

  pages.each do |page|          
    define_method(page) do |a|
      #do nothing
    end
  end
end
user2012677
  • 5,465
  • 6
  • 51
  • 113

2 Answers2

5

You are saying that the method :page1 (:page2 and :page3 too) have one parameter (a), but controller actions don't have parameters, they receive values from the params hash.

EDIT: Simply remove the |a| from your code:

define_method(page) do
  #do nothing
end
Leonel Galán
  • 6,993
  • 2
  • 41
  • 60
2

block without arguments:

class PagesController < ApplicationController

  pages = %w[ page1 page2 page3 ]

  pages.each do |page|          
    define_method(page) do
      #do nothing
    end
  end
end
Oleh Sobchuk
  • 3,612
  • 2
  • 25
  • 41