[Rails] Inherit parameters between controllers using Sessions Helper

Introduction

While creating a flea market app at a certain programming school, the purchase details confirmation page transitions to the credit card registration page. After registering the card, I can no longer move to the confirmation page of the purchase details.

** Cause: The parameter: item_id was 0 when the controller changed from items_controller.rb to credit_cards_controller.rb. ** **

Details

  1. After logging in as a user, use routing to move to the top page (items # index).
  2. Transition to the detail page (items # show) of other users' exhibited products (items # create).
  3. Click the purchase button on the details page (items # show) to move to the product purchase page (items # purchase).
  4. To register / change your credit card information, click the Register / Change button to move to (credit_cards # index).
  5. If there is no change after the transition, click the return button to move to the product purchase page (items #purchase).
  6. If changed / unregistered, move to the card registration page (credit_cards # new, create) and then move to (credit_cards # show). After that, click the card decision / return button to move to the product purchase page (items #purchase).
  resources :items, only: [:index, :new, :create, :show, :edit, :destroy] do
    get '/purchase/:id', to: 'items#purchase', as: :purchase
  end
  resources :credit_cards, only: [:index, :new, :create, :show, :destroy]

スクリーンショット 2020-10-11 13.37.34.png

** 5. An error occurred in 4-1, 4-2 that there is no item_id **

スクリーンショット 2020-10-11 13.58.34.png

When I check it in the terminal, item_id disappears when it straddles the controller.

Processing by ItemsController#purchase as HTML
  Parameters: {"item_id"=>"2"}
  Item Load (0.3ms)  SELECT  `items`.* FROM `items` WHERE `items`.`id` = 2 LIMIT 1
Processing by CreditCardsController#show as HTML

Implementation details

I was searching for a way to take over item_id ...

[Rails] How to use Session Ruby on Rails Tutorial-Session

Judging that session can be used. First, add include Session Helper to application_controller.rb.

application_controller.rb


class ApplicationController < ActionController::Base
  include SessionsHelper #Added the left
end

Create /sessions_helper.rb in / app / helpers and call SessionsHelper with the code below

/app/helpers/sessions_helper.rb


module SessionsHelper
end

Reference article Why is the "Uninitialized constant ApplicationController :: SessionsHelper (NameError)" displayed?

Now the session is ready

First, save item_id in session.

app/controllers/items_controller.rb


def purchase
  @item = Item.find(params[:item_id]) #item from params_Extract id
  session[:item_id] = @item #Item taken out_Save id in session
end

Next, the parameters saved in session are taken out by the controller (credit_cards # index, credit_cards # show) of the credit_card registration destination. When I checked it with binding.pry, it was an array, so I took it out with values.

app/controllers/credit_cards_controller.rb


#session[:item_id]You can retrieve the session data earlier with.
def index
  @item_id = session[:item_id].values.first 
end

def show
  @item_id = session[:item_id].values.first
end

Extract the instance variable (@item_id) created by controller with view

rb:app/views/credit_cards/index.html.haml


= link_to "Return", "/items/#{@item_id}/purchase", class:'return'

rb:app/views/credit_cards/show.html.haml


%button.enter__permit__box.btn(onclick="location.href='/items/#{@item_id}/purchase'")Use the payment method of your choice
= link_to "Return", "/items/#{@item_id}/purchase", class:'return'

Now you can exchange item_id in items # purchase ⇄ credit_cards # show, index. Delete the session when you last purchased the product

app/controllers/items_controller.rb


def pay
  session[:item_id] = nil
end

This time I used session to exchange parameters, but it seems that I can exchange parameters using URL, so I would like to try it next time.

In addition, when using session, there are some risks involved, so please read the link below when using it.

Articles you want to read together (Notes on handling sessions)

Rails Security Guide

Recommended Posts

[Rails] Inherit parameters between controllers using Sessions Helper
[Ruby on Rails] Common processing between controllers (using concaves)
Differences between browser sessions and cookies and Rails session and cookies methods