Home>

I am making a simple diary application using the gem of simple_calender with ruby ​​on rails, but when I try to display it in a browser after automatic deployment with AWS in the production environment, the error of the title appears.

No particular error is displayed in the local environment.
I checked (application name) /current/log/production.log on EC2, but there was no error.
When I checked log/development.log locally,

Started GET "/" for :: 1 at 2020-07-18 00:33:26 +0900
Processing by NotesController # index as HTML
  Rendering notes/index.html.haml within layouts/application
  [1m [36m User Load (3.7ms) [0m [1m [34m SELECT ʻusers`. * FROM ʻusers` WHERE ʻusers`. ʻid` = 1 ORDER BY ʻusers` .ʻid` ASC LIMIT 1 [0m]
  ↳ app/views/notes/index.html.haml: 3
  [1m [36m User Load (0.4ms) [0m [1m [34m SELECT ʻusers`. * FROM ʻusers` [0m]
  ↳ app/views/notes/index.html.haml: 5
  [1m [36mNote Load (0.4ms) [0m [1m [34mSELECT `notes`. * FROM` notes` [0m]
  ↳ app/views/notes/index.html.haml: 29
  Rendered simple_calendar/_month_calendar.html.haml (12.3ms)
  Rendered notes/index.html.haml within layouts/application (42.2ms)
Completed 500 Internal Server Error in 74ms (ActiveRecord: 4.5ms)

ActionView :: Template :: Error (undefined method ʻid'for nil: NilClass):
    16: --week.each do | day |
    17: = content_tag: td, class: calendar.td_classes_for (day) do
    18: --if defined? (Haml)&&respond_to? (: Block_is_haml?)&&block_is_haml? (Block)
    19: --capture_haml (day, sorted_events.fetch (day, []),&block)
    20: --other
    21: --block.call day, sorted_events.fetch (day, [])


As mentioned above, it seems that'id'is not defined, but it is working well in the local environment, so I am in trouble because I can not pursue the cause any more.
I would be very grateful if anyone could give me some advice.

Notes/index.html.haml, which is pointed out as the file in question, is also included for reference.

.header
  .mainContainer
    --if user_signed_in?
      = link_to root_path, class: "mainContainer__title", data: {"turbolinks" =>false} do
        = "# {@ user [current_user.id --1] .nickname} diary"
    --other
      = link_to root_path, class: "mainContainer__title", data: {"turbolinks" =>false} do
        = "Diary"
  .subContainer
    MENU
  .menu # color
    .navigation-block
      % ul.navigation-list
        % li.nav_menu1
          = link_to'Write a diary', new_note_path, data: {"turbolinks" =>false}
        % li.nav_menu2
          = link_to'post list', note_path (@notes), data: {"turbolinks" =>false}
        --if user_signed_in?
          % li.nav_menu3
            = link_to'log out', destroy_user_session_path, method:: delete
        --other
          % li.nav_menu4
            = link_to'login', new_user_session_path
.body
  .sidebar
    .sidebar__text
    --if user_signed_in?
      .sidebar__calendar
        = month_calendar events: @notes do | date, notes |
          = date.day
          % br
          --notes.each do | note |
            --if current_user.id == note.user_id
              = link_to edit_note_path (note), data: {"turbolinks" =>false} do
                = "・"
                = note.title
                % br
    --other
      .sidebar__calendar
        = month_calendar events: @notes do | date, notes |
          = date.day
          % br
  .main
    #existing_diary
    .new_diary # newdiary
      --if user_signed_in?
        = link_to'Write a diary', new_note_path, data: {"turbolinks" =>false}, class: "new_diary__btn"
      --other
        = link_to'Write a diary', new_user_session_path, class: "new_diary__btn"

Please let us know if you have any other necessary information.
Thanks for your cooperation.

  • Answer # 1

    ActionView :: Template :: Error (undefined method ʻid'for nil: NilClass):

    From the log, you can read that current_user is nil (that is, it was not found).
    I think it is necessary to recreate it considering the case where current_user is nil.

Related articles