Search code examples
ruby-on-railscookiescontrolleractionreserved-words

Error generating a controller with a "cookies" action


I'm new programming in Rails and I got an error I don't understand.

When I create a controller with the action cookies like:

rails g controller Pages home about cookies

I get a NoMethodError in PagesController when loading any page:

undefined method `delete' for nil:NilClass Extracted source (around line #9):

def set_request_method_cookie
    if request.get?
      cookies.delete(:request_method)
    else
      cookies[:request_method] = request.request_method
end

I was wondering if the word "cookies" is a reserved word but I don't find it in the list. If not, how can I fix it using the word cookies as an action? Thanks

A screenshot of the error https://i.sstatic.net/WUVKn.png


Solution

  • Yes, this is reserved to interact with browser's cookie.

    You could keep cookies url but route it to a different action name like cookies_action, provide your routes if you want more details. This may help