Rails5.1.3でWebアプリケーション制作の勉強をしています。
ブラウザの表示では「更新」できているように表示されているのですが、データベースの更新がされない原因がわからず困っております。関連するコードは以下になります。
histories_controller.rb def edit @history_form = Admin::HistoryForm.new(History.find(params[:id])) end def update @history_form = Admin::HistoryForm.new(History.find_by(params[:id])) @history_form.assign_attributes(params[:form]) if @history_form.save flash.notice = '発送完了しました。' redirect_to :admin_undispatched_admin_history else flash.now.alert = '入力に誤りがあります。' render action: 'edit' end end
edit.html.erb <% @title = '発送チェック/注文数変更' %> <h1><%= @title %></h1> <div id="generic-form"> <%= form_for @history_form, as: 'form', url:[ :admin, @history_form.history ] do |f| %> <%= render 'form', f: f %> <div class="buttons"> <%= f.submit'更新' %> <%= link_to 'キャンセル', :admin_root %> </div> <% end %> </div>
_form.html.erb <%= FormPresenter.new(f, self).notes %> <fieldset id="history-fields"> <legend>発送状況</legend> <%= render 'history_fields', f: f, confirming: false %> </fieldset>
_history_fields.html.erb <%= f.fields_for :history, f.object.history do |ff| %> <%= markup do |m| p = HistoryFormPresenter.new(ff, self) p.with_options(required: true) do |q| m << q.drop_down_list_block(:undispatched, '発送状況', History::UNDISPATCHED) end end %> <% end %>
history.form.rb class Admin::HistoryForm include ActiveModel::Model attr_accessor :history delegate :persisted?, :save, to: :history def initialize(history = nil) @history = history @history ||= History.new end def assign_attributes(params = {}) @params = params history.assign_attributes(history_params) end private def history_params @params.require(:history).permit(:order, :undispatched) end end
表示はできています。更新ボタンをクリックすると「発送完了しました。」と表示もできていますがカラム値が変わっていないのです。
どなたか教えていただけないでしょうか?宜しくお願いします。
Started PATCH "/admin/histories/2" for 10.0.2.2 at 2018-09-05 16:39:10 +0900 Processing by Admin::HistoriesController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"jG7/Kq/uZFwKrtlud1/cHi0QD87UI1w9jcCXjr/9slgR0LLkerK7RAxY6BmDjqmwMsBXNQkSp61fskpyE+2+JQ==", "form"=>{"history"=>{"undispatched"=>"発送済み"}}, "commit"=>"更新", "host"=>"fujiyoshi.nouen.com", "id"=>"2"} (0.9ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 Administrator Load (0.9ms) SELECT `administrators`.* FROM `administrators` WHERE `administrators`.`id` = 1 LIMIT 1 History Load (0.4ms) SELECT `histories`.* FROM `histories` WHERE (2) LIMIT 1 (1.1ms) BEGIN Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 (0.2ms) COMMIT Redirected to http://fujiyoshi.nouen.com:3000/admin/undispatched_admin_history Completed 302 Found in 90ms (ActiveRecord: 6.6ms) Started GET "/admin/undispatched_admin_history" for 10.0.2.2 at 2018-09-05 16:39:10 +0900 Processing by Admin::HistoriesController#undispatched as HTML Parameters: {"host"=>"fujiyoshi.nouen.com"} Administrator Load (0.5ms) SELECT `administrators`.* FROM `administrators` WHERE `administrators`.`id` = 1 LIMIT 1 Rendering admin/histories/undispatched.html.erb within layouts/admin History Load (0.6ms) SELECT `histories`.* FROM `histories` WHERE `histories`.`undispatched` = '未発送' Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 HomeAddress Load (0.5ms) SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`type` IN ('HomeAddress') AND `addresses`.`customer_id` = 1 LIMIT 1 CACHE Customer Load (0.0ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 [["id", 1], ["LIMIT", 1]] CACHE HomeAddress Load (0.0ms) SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`type` IN ('HomeAddress') AND `addresses`.`customer_id` = 1 LIMIT 1 [["customer_id", 1], ["LIMIT", 1]] Customer Load (1.1ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 2 LIMIT 1 HomeAddress Load (0.4ms) SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`type` IN ('HomeAddress') AND `addresses`.`customer_id` = 2 LIMIT 1 Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 3 LIMIT 1 HomeAddress Load (0.4ms) SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`type` IN ('HomeAddress') AND `addresses`.`customer_id` = 3 LIMIT 1 Rendered admin/histories/undispatched.html.erb within layouts/admin (91.1ms) Rendered admin/shared/_header.html.erb (1.5ms) Rendered shared/_footer.html.erb (0.7ms) Completed 200 OK in 313ms (Views: 301.0ms | ActiveRecord: 7.2ms)
回答1件
あなたの回答
tips
プレビュー