Rails 3 controller private methods as helper method - ruby-on-rails

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 ??

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

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.

No need to mention a private method as helper method in your controller. You can directly call them from another methods from the same controller by passing parameters like params, or any thing.
class ContorllerName < ApplicationController
def index
private_method(params)
end
private
def private_method(vals)
vals
end
end

Related

Call to action on the same controller

I am learning rails and I have a question
How can I call an action from another in the same controller?
def new
new_method()
end
private
def new_method
...
end
This would be the right way?
The parenthesis is optional in Ruby. But, one action receive a call from client and respond one output. Your private "action" is only a function or method.
class User
def create
make_something(params)
end
private
def make_something(params)
#some implementation
end
end

Adding custom internal method to a controller

I'm new with RoR and I have a controller (UsersController) where I wish to verify the existence of a certain session before anything. Since the session verification code is the same for several methods and I don't want to repeat myself, I decided to make a new method in my controller to check the sessions:
class UsersController < ApplicationController
def index
end
def show
end
def new
if self.has_register_session?
# Does something
else
# Does something else
end
end
def edit
end
def create
end
def update
end
def destroy
end
def self.has_register_session?
# true or false
end
end
And when I run the page /users/new, I got this error:
undefined method `has_register_session?' for #<UsersController:0x1036d9b48>
Any idea?
self when you define the method refers to the UsersController class object, but within the instance method new, self refers to the instance of UsersController.
You can either make your method an instance method:
def has_register_session?
# code
end
You can then get rid of the self when calling has_register_session? in new as well.
Or call the method on the class:
if UsersController.has_register_session?
# code
end
instead of referencing UsersController explicitly you could do self.class.
Note that you likely want the first solution: making has_register_session? an instance method.
By doing def self.blah you've created a class method whereas you want an instance method.
You might also want to make the method protected - all public methods are exposed as actions by default.

Can we call a Controller's method from a view (as we call from helper ideally)?

In Rails MVC, can you call a controller's method from a view (as a method could be called call from a helper)? If yes, how?
Here is the answer:
class MyController < ApplicationController
def my_method
# Lots of stuff
end
helper_method :my_method
end
Then, in your view, you can reference it in ERB exactly how you expect with <% or <%=:
<% my_method %>
You possibly want to declare your method as a "helper_method", or alternatively move it to a helper.
What do helper and helper_method do?
make your action helper method using helper_method :your_action_name
class ApplicationController < ActionController::Base
def foo
# your foo logic
end
helper_method :foo
def bar
# your bar logic
end
helper_method :bar
end
Or you can also make all actions as your helper method using: helper :all
class ApplicationController < ActionController::Base
helper :all
def foo
# your foo logic
end
def bar
# your bar logic
end
end
In both cases, you can access foo and bar from all controllers.
Haven't ever tried this, but calling public methods is similar to:
#controller.public_method
and private methods:
#controller.send("private_method", args)
See more details here

Accessing a URL Param Project.find(params[:project_id]) in the application_helper.rb

I'm trying to build a helper called
def current_project
#current_project = Project.find(params[:project_id])
end
I want this so that in the navigation plugin I'm using I can use current_project to populate the navigation.
Problem I'm having is while params[:project_id] is available in the navigation config (navigation.rb) it is not working in the application_helper.rb. I get the following error:
"find_with_ids': Couldn't find Project without an ID"
Here's the navigation plugin, in case you're curious: http://github.com/andi/simple-navigation/
ideas why?
Thanks
If passing in the project_id to the helper it not an option, you can put the method into the controller and mark it as a helper_method. For example:
class ApplicationController < ActionController::Base
helper_method :current_project
private
def current_project
#current_project ||= Project.find(params[:project_id])
end
end
Then in your views you can call it with:
<% current_project %>
as you expect.
Instead of
def current_project
#current_project = Project.find(params[:project_id])
end
Try
def current_project( project_id )
#current_project = Project.find( project_id )
end
When invoking the helper, pass the params[:project_id] for project_id param value.
Definitely agree with rjk, you should put it in application controller, because if you don't your controllers can't access the method. The issue you're getting seems to be due to a nil id (e.g. params[:project_id] isn't passed through in every request) presumably it's only passed in the navigation action which switches between projects?
I'm not sure what the internals of simple navigation do, but if params[:project_id] is only passed in the navigation action, then the navigation action should be storing this in the session session[:project_id] = params[:project_id].to_i, then you would define a private controller method + helper as follows, using the session stored id so you can access it in other actions:
class ApplicationController < ActionController::Base
helper_method :current_project
private
def current_project
#current_project ||= Project.find(session[:project_id])
end
end

how to call a helper method from both view and controller in rails?

I created a helper method for some simple calculation. This helper method will just return an integer. I need the helper in both controllers and views.
Unfortunately, it work well in views but not in controllers. I get the undefined local variable or method error. How can I fix it?
Thanks all
In order to use same methods in both controller and views Add you method in application_controller.rb and make it helper methods.
For example
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
#protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
def current_user
session[:user]
end
end
Now you can use method current_user in both controllers & views
I use a following solution. Because I think helper's methods shoud be stored in an appropriate helper module.
module TestHelper
def my_helper_method
#something
end
end
class SomeController < ApplicationController
def index
template.my_helper_method
end
end

Resources