M
M
Mr Freeman2015-07-02 12:19:09
Ruby on Rails
Mr Freeman, 2015-07-02 12:19:09

How can I make a common view for all controller methods?

There is a controller:

def campaigns
    get_calculate_stats(Campaign)
    render 'stats'
  end

  def sources
    get_calculate_stats(Source)
    render 'stats'
  end

  def offers
    get_calculate_stats(Offer)
    render 'stats'
  end

Is it possible somehow with the help of filters, or somehow do it so as not to write in each action render 'stats', I can not find an answer. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2015-07-02
@vladamir

module Bar

  %w{one two three}.each do |n|

    define_method n do
      super()
      print " BAR"
    end

  end

end

class Foo
  prepend Bar

  def one
    print '1'
  end

  def two
    print '2'
  end

  def three
    print '3'
  end
end

Foo.new.one # 1 BAR
Foo.new.two # 2 BAR
Foo.new.three # 3 BAR

But you don't have to do that. In general, each action should have its own view. And writing render is not that hard.

_
_ _, 2015-07-02
@AMar4enko

I can suggest using the after_action hook
But I'm not a railroader

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question