57
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby on Rails エラーめも

Last updated at Posted at 2017-06-22

エラー

ActionController::UnknownFormat in xxController#xx

スクリーンショット 2017-06-17 13.16.30.png

テンプレートがない。
今回はviews/orders下にindex.html.hamlがなかったのが原因。
該当する場所に適切な名前のビューを作れば解決。

Syntax unexpected keyword_ensure, expecting end-of-input

スクリーンショット 2017-06-19 12.40.29.png

railsの埋め込みに文法ミスあり。
フォームや繰り返し処理など、複数行にわたるもの。

原因:
form_fordo |f|忘れ。

ActiveRecord::StatementInvalid in Devise::RegistrationsController#create

image

"Field 'name' doesn't have a default value"とな。
アカウント新規登録画面で「アカウント作成」をクリックしたらエラー。URLは'/users'。
このURLは間違いだからここのリダイレクトは直さなあかんけど、それ以前のエラー(DB絡み)な気がする。

キター!!これだ原因。

原因:
deviseでは初期状態でサインアップ時にメールアドレスとパスワードのみを受け取るようにストロングパラメーターが設定してあるので、追加したキーのパラメーターは許可されていません。追加のパラメーターを許可したい場合は、application_controllerにおいてbefore_actionconfigure_permitted_parametersメソッドを設定します

例:sign_upアクションに対してnicknameというパラメータを追加で許可する
  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
    before_action :configure_permitted_parameters, if: :devise_controller?

    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname])
    end
  end

ActionController::InvalidAuthenticityToken

image

なにこれ、初めて見た…
Rails4のCSRF対策で「Can't verify CSRF token authenticity」エラーが参考になります。
Railsは外部からのリクエストをはじくようにできてるらしい。

解決策

class ProductsController < ApplicationController
  protect_from_forgery except: :search # searchアクションを除外
  def index
  end

  def search
    @products = Product.where('name LIKE(?)', "%#{params[:keyword]}%")
    redirect_to 'index'
  end
end

undefined method `pry'

image

解決策

  1. gem 'pry-rails' をインストール
  2. サーバー再起動(忘れずに!)

First argument in form cannot contain nil or be empty

image

原因
order_detailオブジェクトが生成されていないから、"nil"は引数に取れないよ〜って言われてる。

解決策
この画面を呼ぶアクションで、事前に@order_detail = OrderDetail.newなど、空のオブジェクトを生成しておく。

57
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
57
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?