###実現したいこと
ruby on railsで確認画面(confirmアクション)と新規作成画面(newアクション)に戻るボタン付きの新規作成機能を実装しています。
アクションを跨がっても変数を維持したいのですが、
confirmからnewに戻ったとき、変数を維持する方法がわからなくて困っています。
###現状の詳細と問題点
Accountのshowページに、取引記録の新規作成画面(account_transactions_conntrollerのnewアクション)へ飛ぶリンクを設置しています。
app/view/account/show.html.erb
<%= link_to "記録作成する", new_account_transaction_path(:account_number => @account1.account_number), class: 'btn btn-danger btn-lg' %>
link_toには、どのアカウントか分かるようにaccount_numberを付与し、account_transactions_controllerのnewアクションに渡して、クラス変数@accountに入れています。
この後、クラス変数@accountに入っているaccount_number(アカウントのレコード)は、
入力画面と確認画面ではアカウント情報表示の為、
createでは新規作成用のフォームオブジェクトにパラメータ(new_account_transaction_form_params)に含まれていない値(account_number)を渡すために
使っています。
class AccountTransactionsController < ApplicationController def new #Accountのshowページのlink_toで生成されたパラメータを元に、アカウントを取得。 @@account = Account.find_by(account_number: account_number_params)#問題の部分,確認画面から戻るとAccountのlink_toからのパラメータがないため中身がない状態になってしまう。 @account = @@account #作成用フォームオブジェクトに受け渡す @account_transaction = NewAccountTransactionForm.new end #取引内容を確認する画面 def confirm #newアクションで取得したクラス変数 @account = @@account @account_transaction = NewAccountTransactionForm.new(new_account_transaction_form_params) return if @account_transaction.valid? render :new end #確認画面で戻るボタンを押したときnew画面に戻れるようにする def back @account_transaction = NewAccountTransactionForm.new(new_account_transaction_form_params) render :new end #取引を作成して記録する def create #新規作成用フォームオブジェクトに渡す @account_transaction = NewAccountTransactionForm.new(new_account_transaction_form_params) #パラメータに含まれていないものをフォームオブジェクトに渡す、newアクションで取得したクラス変数の内容を渡す @account_transaction.account_number = @@account.account_number if @account_transaction.post_transaction redirect_to root_path flash[:notice] = '記録しました' else render :new flash[:notice] = '失敗しました。もう一度試してみてください。' end end private #フォームオブジェクトに渡す用(ユーザーが入力したもの) def new_account_transaction_form_params params.require(:new_account_transaction_form).permit(:amount) end #accountのshowページのlink_toからのパラメータ(ユーザー未入力、自動) def account_number_params params.require(:account_number) end end
app/view/account_transactions/new.html.erb(入力画面)
<p>アカウント番号_<%= @account.account_number %></p> <%= form_for @account_transaction, url: {action: 'confirm'} do |f| %><!-- 確認画面への移動--> <div class="field"> <%= f.label :送金額 %> <%= f.number_field :amount,min:1,max:999999 %> </div> <div class="actions"> <%= f.submit '取引確認画面へ' %> </div> <% end %>
app/view/account_transactions/confirm.html.erb
<h1>確認画面</h1> <p>送金口座番号_<%= @account.account_number %></p> <%= form_for @account_transaction, url: {action: 'create'} do |f| %><ユーザー入力内容表示中、submitボタンが押されるとcreateアクションを実行する用--> <div class="field"> <%= f.label :取引額:, class: "label-inline" %> <%= @account_transaction.amount %> <%= f.hidden_field :amount %> </div> <div class="actions"> <%= f.submit '取引を確定する' %> </div> <% end %> <%= form_for @account_transaction, url: new_account_transaction_path do |f| %><!-- 入力画面に戻る用 --> <%= f.hidden_field :amount %> <%= f.submit '入力画面へ戻る'%> <% end %>
一応上記のコードで、new画面に戻ることはできるのですが、戻ったときに、
Accoutのshowページのlink_toから受け取った値を入れてある@@accountがリセットされてしまい中身がないままになるという問題が発生してしまいます。
確認画面からnew画面に戻ったときは、Accountのshowページからのlink_toパラメータがつくられないことが原因だと思いますが、
もし、この@@accountの内容を維持したい場合はどうすればいいのか、教えて頂ければ幸いです。
###追記(アドバイスに従い修正、ミスあり)
ルーティング設定を変更。
Rails.application.routes.draw do #アカウント関連 resources :accounts, :only => [:index,:show] resources :accounts do resources :transactions, controller: :account_transactions member do post 'confirm', :transactions, controller: :account_transactions#問題の部分 end end #取引記録関連 resources :account_transactions, :only => [:new, :create, :index, :show] resources :account_transactions do collection do post 'confirm'#記録内容確認画面用 end end end コード
###最終追記(アドバイスに従い最終的に成功したコード)
ルーティングの設定です。
resources :users do resources :transactions, controller: :account_transactions do collection do post 'confirm'#確認画面 end end end #このコードにより、user_id(アクションを跨がっても維持したい情報)を含めたURLパターンになる。 #/users/:user_id/transactions/confirm(.:format) #/users/:user_id/transactions/new(.:format)
あとは、各アクションで、
** @account = BasicIncomeAccount.find_by(user_id: params[:user_id])**
と入力すれば、アクションを跨いでも同じ内容を取得できました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/18 12:58 編集
2018/10/18 23:33 編集
2018/10/19 14:17