###前提・実現したいこと
ユーザー編集にて、生年月日の値をdate_selectで入力しています。
Parametersではしっかり値を取得できているのですが、
コントローラーの記入方法がわからず、エラーを起こします。
Error
1NameError in UsersController#update 2uninitialized constant UsersController::Birthday 3@user.sex = params[:sex] 4 @user.birthday = Birthday.new( *赤字枠 5 params[:user]["birthday(1i)"].to_i, 6 params[:user]["birthday(2i)"].to_i, 7 params[:user]["birthday(3i)"].to_i
edit
1<%= form_for(@user, :html => {:multipart => true}) do |f| %> 2 <p>ユーザー名</p> 3 <input name="name" value="<%= @user.name %>"> 4 <p>生年月日</p> 5 <%= f.date_select :birthday, { 6 start_year: 1930, 7 end_year: (Time.zone.now.year)}%> 8 <input type="submit" value="保存">
Controller
1def update 2 @user = User.find_by(id: params[:id]) 3 @user.name = params[:name] 4 @user.birthday = Birthday.new( 5 params[:user]["birthday(1i)"].to_i, 6 params[:user]["birthday(2i)"].to_i, 7 params[:user]["birthday(3i)"].to_i 8) 9 if @user.save 10 redirect_to("/users/#{@user.id}") 11 else 12 render("users/edit") 13 end
Parameters
1"name"=>"aaaaaa", 2"user"=>{"birthday(1i)"=>"1989", "birthday(2i)"=>"1", "birthday(3i)"=>"1"},
Schema
1create_table "users", force: :cascade do |t| 2 t.string "name" 3 t.date "birthday" 4 end
このようなハッシュのパラメータの正しい保存方法をお教えください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/13 23:42