前提・実現したいこと
form_forを用いてrewardsテーブルで保存される入力フォームを作成途中です。しかしながらコントローラー内のcreateまでデータは飛んできている(binding.pryで確認済み)のですが、DBにデータが保存されません。エラーメッセージも出ないため改善できずにいます。どなたかお力添えをしていただけると大変助かります。
該当のソースコード
newhamlhtml
1 2=form_for (@reward) do |f| 3 = f.text_field :name, class: "chat-group-form__input" 4 5 = f.submit "Save", class: "chat-group-form__action-btn" 6
rewardsController
1 2 3class RewardsController < ApplicationController 4 5 6 def new 7 @reward = Reward.new 8 end 9 10 def create 11 @reward = Reward.new(reward_params) 12 if @reward.save 13 redirect_to root_path, notice: 'グループを作成しました' 14 else 15 render :new 16 end 17 18 end 19 20 private 21 def reward_params 22 params.require(:reward).permit(:name) 23 end 24 25end
dbrewardsrb
1class CreateRewards < ActiveRecord::Migration[5.0] 2 def change 3 create_table :rewards do |t| 4 t.string :name, null: false 5 t.index :name, unique: true 6 t.references :user, foreign_key: true 7 t.timestamps 8 end 9 end 10end 11
modelrewardsrb
1class Reward < ApplicationRecord 2 belongs_to :message 3 belongs_to :user 4 validates :name, presence: true 5 validates :user_id, presence: true 6 7end 8
params
1{"utf8"=>"✓", "authenticity_token"=>"9xWjmMo19K5V51n9rnaC+uw6AeWkpq9ngrK75wqLU0m7f2ESCFAyss+v9KlYx3A1//EDfnAjwMdvcYSxnpZOMQ==", "reward"=><ActionController::Parameters {"name"=>"コーヒー"} permitted: false>, "commit"=>"Save", "controller"=>"rewards", "action"=>"create"} permitted: false>
試したこと
コントローラーのcreateアクションやストロングパラメータなど記述を見直しましたが改善されませんでした。form_forはハッシュ式でデータが飛ばされているためprivateメソッドに問題があるのかと思いましたがparamsではデータの中身がちゃんと受け取れているためどこに問題があるのかがわかりません。申し訳ございません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。