Undefined helper method after upgrading to Rails 4.1.1 - ruby-on-rails

I'm having trouble accessing a helper method after upgrading to Rails 4.1.1. I have the following code in my application.
module ApplicationHelper
def last_page_url
session[:last_page]
end
end
class Admin::ArticlesController < ApplicationController
def update
#....more code here
return redirect_to self.last_page_url
end
end
In Rails 4.0.x this code worked fine. After upgrading to Rails 4.1.1 I'm getting an error "undefined method 'last_page_url' whenever my update action runs. Why is this breaking now?

I'm not really sure why this stopped working after upgrading to Rails 4.1.1, but as #steel suggested, it had something to do with the helper method not being included in my particular controller. Adding include ApplicationHelper to the top of my Controller would have worked and I probably could have taken it a step further by adding it to the ApplicationController class since I needed that method available to all controllers. In the end I opted for a different solution:
First, I moved the last_page_url from the ApplicationHelper to the ApplicationController class so all of my controllers could access it. Then I used helper_method to make this method available to all my views. My final code is as follows:
module ApplicationHelper
end
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def last_page_url
session[:last_page]
end
helper_method :last_page_url
end
If anyone ever figures out something changed from Rails 4.0 to Rails 4.1 I would be interested in learning what happened. In this particular application I'm just using the default Rails 4.1 settings in my development.rb.

Related

Rails Controller Namespace Inheritance Missing Layout

Using a namespaced controller and the app's Application layout isn't being loaded.
# controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
# controllers/servers/base_controller.rb
module Servers
class BaseController < ApplicationController
def initialize
# I noticed params are not accessible here either...
# Only in the child controller.
end
end
end
# controllers/servers/test_controller.rb
module Servers
class TestController < BaseController
def index
end
end
end
A pretty basic setup here. Anytime I route to /servers/:id it loads the page with the parameter, but the application layout does not load. I have a series of a couple controllers I'd like to all inherit from this BaseController to initialize a few things. I also noticed that besides the layout issue, the BaseController doesn't have access to url parameters. The TestController does though... Can anyone explain these two issues I'm seeing?
Rails 5.1.4
ruby 2.4.1p111
Figured out my issue. Don't use an initialize method in your class. Rails doesn't like that. Use before_action instead.

Render a view in Rails 5 API

I generated an API-only rails app with Rails 5 via rails new <application-name> --api. I've decided I want to include a view for testing some things and am having issues getting a view to load.
I created a users/index.html.erb file with some text and my controller is now simply def index; end but there is nothing appearing when I hit the /users URL. I also tried commenting out the # config.api_only = true in config/application.rb but that didn't affect anything. Any suggestions on how to proceed?
You don't need to uncomment config.api_only = true for this purpose, just inherit your controller from ActionController::Base, or do it in your ApplicationController (default for common rails generation).
Code:
For this controller only YourController < ActionController::Base
For all apllication ApplicationController < ActionController::Base
this is from the ActionController::Metal docs https://apidock.com/rails/ActionController/Metal
it says:
ActionController::Metal by default provides no utilities for rendering >views, partials, or other responses aside from explicitly calling of >response_body=, content_type=, and status=. To add the render helpers >you’re used to having in a normal controller, you can do the following:
class HelloController < ActionController::Metal
include AbstractController::Rendering
include ActionView::Layouts
append_view_path "#{Rails.root}/app/views"
def index
render "hello/index"
end
end
So I've tried it myself and adding just by adding the two modules actually work just fine when using it for ActionController::API

Using helper in controller in Rails 4.2.4

I'm confused by the rails documentation that I'm reading here. In particular, this sentence:
By default, each controller will include all helpers. These helpers
are only accessible on the controller through .helpers
What is this .helpers that it is referring to? I have a helper defined in app/helpers/areas_helper.rb:
module AreasHelper
def my_helper
puts "Test from helper"
end
end
I would like to use this helper in app/controllers/locations_controller.rb:
class LocationsController < ApplicationController
def show
helpers.my_helper
end
end
However, I get a method undefined error. How is this .helpers supposed to be used?
I know there are other ways to get access to helpers in controllers, but I'm specifically asking about this piece of documentation and what it's trying to say.
You're meant to include the helper class in the controller:
#app/controllers/locations_controller.rb
class LocationsController < ApplicationController
include AreasHelper
def show
my_helper
end
end
This feature was introduced in Rails 5 with following PR
https://github.com/rails/rails/pull/24866
So, we can use this feature from Rails 5 and onwards and not in Rails 4.x.

Rails 3 helper methods not available in controllers even after explicitly including them

Fist of all I'm newbie to rails.
I have this in SessionsController:
def method
sign_in 'user'
end
And this in SessionsHelper:
def sing_in(user)
......
end
So Googling and reading some answers on stackoverflow let me to try something like this:
Including the 'SessionsHelper' to the SessionsController and even tried to put it in 'ApplicationController' like this:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
def handle_unverified_requests
sign_out
super
end
end
But I'm getting
NoMethodError : undefined method `sign_in' for SessionsController:0x007eff2004fcd8
Also few questions:
1) What's the point/difference in putting few methods in Heplers and few in Controllers? Is it a security issue or what?
2) I also tried puting the sign_in method in the SessionsController as i read in stackoverflow that methods defined in controllers can be accessed in views. so to avoid any problems in the views I used
helper_method
but still the same NoMethodError
3) THe best and easy way to access the helper methods in controllers?
So where am I going wrong.
I'm using Rails 3.2.11 and Ruby 2.0.0p0
Looks like a typo: your helper method is 'sing_in' instead of 'sign_in'.

Missing helper "devise_controller?" in ApplicationController

I wanted to set layout to my Devise sign-in page, so I added the following to my ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery
if devise_controller?
layout "single_column_with_banner"
end
end
However, I get the following error:
undefined method `devise_controller?' for ApplicationController:Class
I can see that the helper method is specified in devise.rb, but I just can't access it from application controller. I'm using Rails 3.2 (in process of migration from 2.3). Any clues where I should look at? As a last resort I can override SessionsController but I don't really want to do that yet.
It seems like layout can be specified without either device_controller? or overriding SessionsController. In order to specify layouts, place the following in application.rb:
config.to_prepare do
Devise::SessionsController.layout "single_column_with_banner"
end

Resources