Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 controller private methods as helper method

is it necessary to mention the private methods in controller as helper_methods in controller? Like

class PostsController < ApplicationController
  helper_method :check_something

  def new
    check_something
    @post = Post.new
  end

  def show
    @post = Post.find(params[:id])
  end

private

  def check_something
    redirect_to(root_path) and return if something
  end
end

Is the statement : helper_method :check_something required ? if so why ?

And when i call a private method from a controllers action method is the params hash accessible in the private or the helper method ??

like image 464
AshwinKumarS Avatar asked Dec 03 '22 00:12

AshwinKumarS


2 Answers

I think you have misunderstood the 'helper_method' concept.

helper_method is used to make your controller method to act as a method as its in your helper modules

So inside your controller, you can always access your private method without the 'helper_method' section

and if you add a controller method as a helper method, as you have already done, in your view you can simply call it

and for your second question, yes params hash is accessible via controllers private methods

HTH

like image 176
sameera207 Avatar answered Jan 05 '23 23:01

sameera207


No it is not necessary. You can always call private methods of your controller within your controller.

Also, params would be available automatically for the private methods within controller.

like image 42
Subodh Avatar answered Jan 05 '23 23:01

Subodh