RailsのparamsはString

備忘録として。

はまった、というか初めて気づきました。タイトル通りRailsparamsStringなんですね。

事の経緯

以下のようなコントローラーがあったとします。

class ItemsController < ApplicationController
    def new
        @date = params[:date]
    end
end

それに対するspecが以下

require 'rails_helper'

RSpec.describe ItemsController, :type => :controller do
    describe "GET #new" do
        it "assigns the requsted date to @date" do
            get :new, date: 1
            expect(assigns(:date)).to eq 1
        end
    end
end

結果がこちら

f:id:koyamay:20150104212617p:plain

ナンデString!?

そういうもんだった

つまり、

  1. params[:hoge]は文字列で帰ってくる
  2. findメソッドは文字列でも指定することができる

ので普段あまり気づかなかったのですね。rspec書き始めて初めて気づきました。勉強になりました。