今日のきろく 100DaysOfCode(51/100)

Ruby on Railsメモ

  • cloud9でWebアプリを立ち上げてエラーの際に、ブラウザからエラー内容が確認できない時は、 log/development.log から確認できる。

  • Bootstrap適用後にハマる。

Started GET "/" for 103.5.142.233 at 2018-10-17 08:05:00 +0000
Processing by MessagesController#index as HTML
  Rendering messages/index.html.erb within layouts/application
  Rendered messages/index.html.erb within layouts/application (0.8ms)
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)

このエラーからindex#htmlが怪しいと見抜くことがポイントだった。

  • エラー2
Cannot render console from 103.5.142.233! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

config/application.rb/ を以下のように変更すると治った。なぜ?

module MessageBoard
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.web_console.whitelisted_ips = '103.5.142.233'
  end
end

-その3

app/controllers/messages_controllers.rb 40行目を間違えて message_url としてしまった。

def destroy
    @message.destroy
    
    flash[:success] = 'Messageは正常に削除されました'
    redirect_to message_url
  end

すると以下のエラーが出てきた。なぜだか分からない。 set_messagebefore_action なのに、なぜそれより後に処理されるdef destroy の中身が影響するのか?

ActiveRecord::RecordNotFound (Couldn't find Message with 'id'=4):
  
app/controllers/messages_controller.rb:50:in `set_message'
  Rendering /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.1ms)
  Rendering /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.4ms)
  Rendering /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
  Rendered /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (20.2ms)

Image from Gyazo

-これも知らんかった。

<%= link_to 'Show Page', @blog %>
<%= link_to 'Update Page', @blog, :method => :put %>
<%= link_to 'Destroy Page', @blog, :method => :delete %>

「show」「update」「destroy」アクションのように「id」パラメータを指定して呼び出すアクションの場合は、モデルオブジェクト(下記では@blog変数に格納されている)を使って次のように簡略的に記述することができます。(「edit」アクションは除く)。