Ruby/Rails: Why is render json: {hello: 'world'} hitting my database? - ruby-on-rails

I have an action:
def test
_process_action_callbacks.map { |c| pp c.filter }
render json: {hello: 'world'}
end
That for some reason is calling my current_user function defined in my application controller.
At first I thought it was a before action that was calling my current_user function (hence _process_action_callbacks). But after stripping away all of my before actions the call remained. The only two before actions are part of rails:
:clean_temp_files
:set_turbolinks_location_header_from_session
I used caller to see where my method was getting called from. Here's the stacktrace (and method declaration):
def current_user
pp caller
# get the current user from the db.
end
As you can see, the current_user function is being called by the serialization_scope method in the serialization class. How do I prevent it from calling my current_user function?

Your tag indicates you are using active-model-serializers. By default current_user is the scope. To customize the scope, defined in the application-controller, you can do something like
class ApplicationController < ActionController::Base
serialization_scope :current_admin
end
The above example will change the scope from current_user (the default) to current_admin.
In your case, you probably just want to set the scope in your controller (I assume it is called SomeController ;) ) you can write
class SomeController < ApplicationController
serialization_scope nil
def test
render json: {hello: 'world'}
end
end
See for complete documentation: https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope

Related

How to access a controller method from a jbuilder template

I call a method on controller A that renders a jbuilder template (a file called signed_in.json.jbuilder). Inside the template I want to call a method, friendsCount, that is in controller B. I'm getting an error when I try to access the controller B method inside the template. The error is:
NameError - undefined local variable or method `friendsCount' for #<#<Class:0x007f90a09994f8>:0x007f90a0998788>:
app/views/api/sessions/signed_in.json.jbuilder
I thought that adding helper_method: friendsCount to controller B should solve exactly the type of issue I want to solve: making a controller method available in views.
Actually, when I try to access a method from my ApplicationsController in my jbuilder template (controller C below), I have no issue.
Here is my code:
CONTROLLER A:
class Api::SessionsController < ApplicationController
def create
#user = User.find_by_credentials(session_params[:username], session_params[:password])
if #user.nil?
render json: ["couldn't find user"], status: 404
else
sign_in!(#user)
render :signed_in
end
end
private
def session_params
params.require(:session).permit(:username, :password)
end
end
JBUILDER TEMPLATE: signed_in.json.jbuilder
json.friendsSorted sortFriends
json.friends friendsCount
CONTROLLER B
class Api::FeedsourcesController < ApplicationController
helper_method :friendsCount
def friendsCount
friendsCountHash = {}
# ... populate friendsCountHash
render json: friendsCountHash
end
CONTROLLER C
class ApplicationController < ActionController::Base
helper_method :sortFriends
def sortFriends
#...
return sortedFriendsArray
end
end
I do not want to have the friendsCount method in the helpers folder because I also need to be able to have a direct controller route to this method for other purposes.
You need to have helper methods in the calling controller or its superclass.
In your case it's SessionsController and ApplicationController.
FeedsourcesController is not superclass for SessionsController, so view renderer do not know about any methods in it.

Disable auto rendering in rails

What I need is to disable the automatic page (HTML) rendering in rails and override it with a after_action method. What I'm trying to achieve is an equivalent of CakePHP $this->autoRender = false;
application_controller.rb
class ApplicationController < ActionController::Base
after_action :custom_render
layout nil # Tried this but didn't worked
def custom_render
render #[...]
end
end
some_controller.rb
class SomeController < ApplicationController
def index
# No rendering here
end
end
As shown in the code I tried to add a layout nil to prevent all actions from rendering, but that doesn't seem to affect the behaviour of the action.
Haven't checked whether it works with Rails 4, but this patch works for Rails 5.
According to the code of BasicImplicitRender and ImplicitRender, send_action of is BasicImplicitRender responsible for calling default_render
Documentation says:
For API controllers, the implicit response is always 204 No Content.
For all other controllers, we use ... heuristics to decide whether to
render a template, raise an error for a missing template, or respond with
204 No Content ...
So I suppose redefining default_render method will serve you purpose.
In your controller:
def a
# uses `default_render` unless you call `render` method explicitly
end
def b
render plain: 'Custom text for b' # `default_render` won't be called
end
private
# This does the trick
#
def default_render
render plain: 'Text'
end
You may also hack send_action just like it is done in Rails so as to even skip default_render call at all:
module ActionController
module BasicImplicitRender # :nodoc:
def send_action(method, *args)
# super.tap { default_render unless performed? }
super
end
end
end
To disable rendering (well return nothing) issue.
def index
render :nothing
end
But it's too late to do anything, as it will return response with empty body.
To disable layout:
def index
render layout: false
end
This will render you view without a layout, issue (render layout: 'my_custom_layout') to render default view but with different layout.
We don't know what you want, but the simplest solution is just to render a specific view, f.i.:
def index
render 'my_custom_file.'
end
There are really many options: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
EDIT - as requested in a comment
class ApplicationController < ActionController::Base
before_action :set_user_template
# ...
def set_user_template
template_name = current_user.template_name
self.class.layout "#{template_name}/application"
end
end

RoR: instances variables within controller methods

My question is about controller methods (possibly included from an outside class) that work with instance variables. I frequently use a before_filter in controllers to set up certain variables, e.g.:
class DocumentController < ApplicationController
before_filter :fetch_document
def action
#document.do_something
end
private
def fetch_document
#document = Document.find(params[:id])
end
end
I've been working on a project in which a few controllers will share some functionality, say, document editing. My first thought was to extract the relevant methods, and get them from application_controller.rb or a separate module. But then I noticed I was writing code that looks like this:
def fetch_document
#document = Document.find(params[:id])
end
def do_something_to_document
#document.do_something
end
This sets off warning bells: do_something_to_document is essentially assuming the existence of #document, rather than taking it as an argument. Is this, in your sage opinions, a bad coding practice? Or am I being paranoid?
Assuming it is an issue, I see two general approaches to deal with it:
Check for the instance var and bail unless it's set:
def do_something_to_document
raise "no doc!" unless #document
[...]
end
Call the action with the instance var as an argument:
def do_something_to_document(document)
[...]
end
2 looks better, because it hides the context of the calling object. But do_something_to_doc will only be called by controllers that have already set up #document, and taking #document as a method argument incurs the overhead of object creation. (Right?) 1 seems hackish, but should cover all of the cases.
I'm inclined to go with 1 (assuming I'm right about the performance issue), even though seeing a list of methods referencing mysterious instance vars gives me hives. Thoughts? Let me know if I can be more clear. (And of course, if this is answered somewhere I didn't see it, just point me in the right direction...)
Thanks,
-Erik
If you really need document in different controllers, I'd do something like this:
class ApplicationController < ActionController::Base
private
def document
#document ||= Document.find(params[:document_id])
end
end
class FooController < ApplicationController
before_filter :ensure_document, :only => [:foo]
def foo
document.do_something
end
private
# TODO: not sure if controller_name/action_name still exists
def ensure_document
raise "#{controller_name}##{action_name} needs a document" unless document
end
end
As #variable are session/instance variable you will get a Nil exception in do_something_to_document method.
The first code is fine, because before_filter will always load your #document.
I suggest you to write something like that
def fetch_document(doc_id)
#document ||= Document.find(doc_id)
end
def do_something_to_document
my_doc = fetch_document(params[:id])
end
where do_something_to_document is in the controller (if not, dont use params[:id], even if you know you can access this global, use another explicit parameter). The ||= thing, will asssure that you call the base only once by request.

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.

How to pass values between controller methods

Is there any way to share an array between controller methods and store it until page reloads or calling method of another controller? Some methods should change the array.
you can use rails cache.
Rails.cache.write("list",[1,2,3])
Rails.cache.read("list")
If you want to share the value across the methods of a same controller instance then,
declare an instance variable:
class BarsController < UsersController
before_filter :init_foo_list
def method1
render :method2
end
def method2
#foo_list.each do | item|
# do something
end
end
def init_foo_list
#foo_list ||= ['Money', 'Animals', 'Ummagumma']
end
end
If you want to share the value across two controllers withn a session, then:
class BarsController < UsersController
before_filter :init_foo_list
def method1
render :controller => "FoosController", :action => "method2"
end
def init_foo_list
params[:shared_param__] ||= ['Money', 'Animals', 'Ummagumma']
end
end
class FoosController < UsersController
def method2
params[:shared_param__].each do | item|
# do something
end
end
end
Give an unique name to the shared parameter key so as to avoid collision with existing keys.
Other option is to store the shared array in the session ad delete it before the final render.
I am not sure whether my answer is close to your requirement, but this is what I do if I want to get the value of an object/model which is fetched in one controller action and basis on that value I need to fetch other values in another controller action.
I make use of class variables and use it throughout my controller action
for eg:
#pages=Post.find.all`
##my_value=#pages.(any manipulations)
now ##my_vales can be used in any actions of that controller..
hope it helps...

Resources