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. ** **
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]
** 5. An error occurred in 4-1, 4-2 that there is no item_id **
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
I was searching for a way to take over item_id ...
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.