Questions tagged [applicationcontroller]
61 questions
45
votes
12 answers
"undefined method" when calling helper method from controller in Rails
Does anyone know why I get
undefined method `my_method' for #
when I call my_method("string") from within my ApplicationController subclass? My controller looks like
class MyController < ApplicationController
def show
…

Chad Johnson
- 21,215
- 34
- 109
- 207
38
votes
3 answers
How to set cookies in ApplicationController?
I need to set cookies in my ApplicationController but I'm not sure how. I've tried using cookies - nothing, using ActionController::Cookies - nothing.
I don't need anything more then setting and getting cookies but what I do need is to set them in…

AndrewShmig
- 4,843
- 6
- 39
- 68
20
votes
3 answers
'protect_from_forgery' in Application controller in Rails
In the config/application_controller.rb file in my Rails application directory, I found the code below:
class ApplicationController < ActionController::Base
protect_from_forgery
end
Can any one tell me what project_from_forgery means and why it…

Rajesh Omanakuttan
- 6,788
- 7
- 47
- 85
19
votes
4 answers
How can I extend ApplicationController in a gem?
I thought I'd come up with a slick way to extend ApplicationController in a Rails 3.x gem.
In my gem's lib/my_namespace/my_controller.rb, I had:
class MyNamespace::MyController < ApplicationController
before_filter :some_method
after_filter…

Gary S. Weaver
- 7,966
- 4
- 37
- 61
18
votes
6 answers
How do you route an action to the application controller in Rails 3?
I am using the gem rails3-jquery-autocomplete and had no problems with it, however I have now moved my autocomplete form into the application template and therefore the ajax calls are now being dealt by the application controller, so my routes have…

Hard-Boiled Wonderland
- 1,359
- 3
- 17
- 32
5
votes
1 answer
Rails: In Application Controller, force login, redirect all requests except login
I'd like a simple method in my application controller that requires all users to log in before continuing to any part of the site. I'm using Devise for authentication.
I tried:
class ApplicationController < ActionController::Base
...
unless…

Andrew
- 42,517
- 51
- 181
- 281
5
votes
2 answers
Rails: rendering json with multiple objects
I am trying to return multiple objects with the call:
def index
beers = Beer.all
micros = Micros.all
render json: {beers: beers, micro: micros}
end
However, for both objects is only returning the attributes listed in the respected…
user2876886
5
votes
2 answers
Rails 3.0 Engine - Execute code in ActionController
I am upgrading my Rails plugin to be an engine that works with the latest 3.0RC1 release and I'm having a bit of trouble figuring out the best (and most correct) way to extend ActionController. I've seen this post by DHH and this question here on…

Topher Fangio
- 20,372
- 15
- 61
- 94
4
votes
2 answers
How to pass an empty text array from view to controller for update in Rails?
I have a Model that has a text array field defined like this
t.text "widgets", default: [], array: true and a permitted param in my controller
params.require(:model).permit(widgets: [])
In my view I can build values to put into this array using…

JuJoDi
- 14,627
- 23
- 80
- 126
3
votes
2 answers
DRY-ing ASP.NET MVC actions: ApplicationController or Service?
I'm writing an ASP.NET MVC 3 app and I'm finding myself writing this line rather often in my action methods:
var user = _session.Single(u => u.UserName == User.Identity.Name);
(Obviously used in conjunction with the AuthorizeAttribute)
There…

Sergi Papaseit
- 15,999
- 16
- 67
- 101
3
votes
1 answer
How to merge default values into nested params in Rails 5?
I am desperately trying to merge a set of default values into my nested params. Unfortunately, using deep_merge no longer works in Rails 5 since it no longer inherits from Hash.
So this does not work:
class CompaniesController <…

Tintin81
- 9,821
- 20
- 85
- 178
2
votes
0 answers
How can I exactly simulate an HTTP request with ApplicationController.render?
I am trying to render from outside a controller exactly what my Rails app renders from within a controller when a user sends an HTTP request with specific headers and parameters.
ApplicationController.renderer is pretty good for this, however it…

Tom Lehman
- 85,973
- 71
- 200
- 272
2
votes
2 answers
Rails before_action not called on reload
I have a Rails application (5.2.4.1) deployed in a subdirectory on a WordPress site. My controllers calls a before_action method to authenticate users
class ClientsController < ApplicationController
before_action :authenticate unless…

mrturtle
- 609
- 1
- 5
- 14
2
votes
0 answers
Adding Logstasher custom fields in ApplicationController
I am trying to log additional custom fields using Logstasher gem.
I need to log some response parameters, but logstasher does not support itself logging response params using configuration, therefore I put the code into after_action method in…

twinster
- 86
- 3
2
votes
0 answers
redirect_to in rails not behaving
I am authorizing and redirecting in my application controller but the redirect_to method is not behaving. I am using rails '4.2.6', and I am not sure if we used 'api mode' or simply stripped the extra stuff out as we are using the rails app as our…

Justin Pease
- 21
- 2