'Getting undefined method errors with fresh ruby project
I've installed ruby on rails on my raspberry pi (following this tutorial). I've created a new project and generated a controller with 3 actions. Thats all I did. When I run the server and navigate to one of the 3 actions, I always get this error:
NoMethodError in VolumeController#overview
undefined method `allow_forgery_protection' for nil:NilClass
I am new to ruby on rails but what I've already tried is to comment out the "protect_from_forgery with: :exception" line on my ApplicationController. But after doing so I get the following error on all actions:
NoMethodError in VolumeController#overview
undefined method `inheritable_copy' for nil:NilClass
I don't understand where this could come from. There is absolutely no code in the controllers and views yet, everything is still as it was generated by rails with the "rails g controller" command.
I then tried to generate a scaffold with rails in a new project and this works! But thats not what I want. I also tried to create my project and controller again but I still get the same error.
Is anybody out there who can help me? What did I do wrong?
Solution 1:[1]
I have the same error, and it was caused because i was defined a method call 'config'. I change the name and it was solved. :)
Solution 2:[2]
I just got burned by this with Rails 7, so it's still valid. In my case, it was during a controller test using minitest. The stack trace can be misleading and I had trouble understanding how the existing answer was relevant.
Error: PostsControllerTest#test_should_destroy_post: NoMethodError: undefined method `allow_forgery_protection' for nil:NilClass
def allow_forgery_protection; config.allow_forgery_protection; end ^^^^^^^^^^^^^^^^^^^^^^^^^ test/controllers/posts_controller_test.rb:18:in
block (2 levels) in <class:PostsControllerTest>' test/controllers/posts_controller_test.rb:17:inblock in class:PostsControllerTest'
I was using a convenience method called config, unaware of its reserved status, and without any issues until ActionDispatch::IntegrationTest
# app/controllers/application_controller.rb
def config
Rails.configuration.general
end
# Ruby 3 end-less method style
def config() = Rails.configuration.general
Oddly enough, I can't find any source documentation, just reserved words maintained by individuals. This one is on me though, really shouldn't be putting non-RESTful non-protected methods like this in the first place. There is actually a note warning about this in the guides.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Franco |
| Solution 2 | Ryan Romanchuk |
