Home>

I'm creating a clone app for flea market apps in Rails.
When purchasing a product, I want to be able to save the delivery address (address) information of the product and the information of the purchased product in each table at the same time.

I thought as follows, but in this description, I get the error "param is missing or the value is empty: order_address" and cannot pass the information well.

In the Form object, first save the information sent as parameters from the form in the table.
order_address.rb

class OrderAddress
  include ActiveModel :: Model
 attr_accessor: postal_code,: prefecture_id,: city,: address_num,: building_name,: phone,: item.id,: params [: token],

 : current_user.id
  with_options presence: true do
    validates: postal_code, format: {with:/\ A [0-9] {3}-[0-9] {4} \ z /, message: "is invalid. Include hyphen (-)"}
    validates: city, format: {with:/\ A [a-n-n-ichi- 龥] /, message: "is invalid. Input full-width characters."}
    validates: address_num
    validates: phone, format: {with:/\ A \ d {10} \ z | \ A \ d {11} \ z /, message: "is invalid."}
    validates: token
  end
    validates: prefecture_id, numericality: {other_than: 0, message: "can't be blank"}

    def save
      Order.create (item: id)
      Address.create (postal_code: postal_code, address_num: address_num, building_name: building_name, phone: pOrder.create (user: current_user.id, item: id)
      Address.create (postal_code: postal_code, prefecture_id: prefecture_id, city: city, address_num: address_num, building_name: building_name, phone: phone) hone)
    end
end


Next, create an instance of the Form object with the new action and create action of the controller.
orders_controller.rb

class OrdersController</pre>
<p><br />
(PAY.JP also has a credit card payment function.)<br />
card.js</p>
<pre><code data-language = "Ruby">const pay = () =>{
  Payjp.setPublicKey (process.env.PAYJP_PUBLIC_KEY);
  const form = document.getElementById ("charge-form");
  form.addEventListener ("submit", (e) =>{
    e.preventDefault ();
    const formResult = document.getElementById ("charge-form");
    const formData = new FormData (formResult);
    const card = {
      number: formData.get ("order [number]"),
      exp_month: formData.get ("order [exp_month]"),
      exp_year: `20 ${formData.get ("order [exp_year] ")}`,
      cvc: formData.get ("order [cvc]"),
    };
    Payjp.createToken (card, (status, response) =>{
      if (status == 200) {
        const token = response.id;
        const renderDom = document.getElementById ("charge-form");
        const tokenObj = `<input value = ${token} name ='token' type =" hidden ">`;
        renderDom.insertAdjacentHTML ("beforeend", tokenObj);
      }
      document.getElementById ("card-number"). removeAttribute ("name");
      document.getElementById ("card-exp-month"). removeAttribute ("name");
      document.getElementById ("card-exp-year"). removeAttribute ("name");
      document.getElementById ("card-cvc"). removeAttribute ("name");
      document.getElementById ("charge-form"). submit ();
    });
  });
};
window.addEventListener ("load", pay);

Below is a view of the screen to be entered from the form.
I am trying to store address information (postal_code, prefecture_id, city, address_num, building_name, phone, order_id) in the adresses table and purchase information (user_id, item_id) in the orders table.

          Confirmation of purchase details
      <% # Display of purchase details%>      <% = image_tag "item-sample.png", class:'buy-item-img'%>                    <% = "Product name"%>                      <p class ='item-price-text'>\<% = "999,999,999"%></p>      <p class ='item-price-sub-text'>(tax included) Shipping included</p>            <% #/Display of purchase details%><% # Display of payment amount%>                payment
          <p class ='item-payment-price'>      \<% = "Sales price"%>  </p>    <% #/Display of payment amount%><% = form_with (model: @order_address, url: item_orders_path, id:'charge-form', class:'transaction-form-wrap', local: true) do | f |%><% #Enter card information%>                Credit card information input
                            <label>Card information</label>      <span>Required</span>            <% = f.text_field: number, class: "input-default", id: "card-number", placeholder: "card number (half-width alphanumeric characters)", maxlength: "16"%>              <% = image_tag'card-visa.gif', class:'card-logo'%>      <% = image_tag'card-mastercard.gif', class:'card-logo'%>      <% = image_tag'card-jcb.gif', class:'card-logo'%>      <% = image_tag'card-amex.gif', class:'card-logo'%>                                  <label>Expiration date</label>      <span>Required</span>                      <% = f.text_area: exp_month, class: "input-expiration-date", id: "card-exp-month", placeholder: "Example) 3"%>      <p>Month</p>      <% = f.text_area: exp_year, class: "input-expiration-date", id: "card-exp-year", placeholder: "Example) 23"%>      <p>year</p>                                  <label>Security code</label>      <span>Required</span>            <% = f.text_field: cvc, class: "input-default", id: "card-cvc", placeholder: "4 or 3 digit number on the back of the card", maxlength: "4"%>          <% #/Enter card information%><% # Enter shipping address%>        Enter delivery address
                            <label>Zip code</label>      <span>Required</span>            <% = f.text_field: postal_code, class: "input-default", id: "postal-code", placeholder: "Example) 123-4567", maxlength: "8"%>                          <label>Prefectures</label>      <span>Required</span>            <% = f.collection_select (: prefecture_id, Prefecture.all,: id,: name, {},

 {class: "select-box", id: "prefecture"})%>                          <label>Municipality</label>      <span>Required</span>            <% = f.text_field: city, class: "input-default", id: "city", placeholder: "Example) Midori-ku, Yokohama"%>                          <label>address</label>      <span>Required</span>            <% = f.text_field: address_num, class: "input-default", id: "addresses", placeholder: "Example) 1-1-1 Aoyama"%>                          <label>Building name</label>      <span>optional</span>            <% = f.text_field: building_name, class: "input-default", id: "building", placeholder: "Example) Yanagi Building 103"%>                          <label>phone number</label>      <span>Required</span>            <% = f.text_field: phone, class: "input-default", id: "phone-number", placeholder: "Example) 09012345678", maxlength: "11"%>          <% #/Enter shipping address%>      <% = f.submit "Purchase", class: "buy-red-btn"%>    <% end%>

I'm very sorry, but could you please tell me what the problem is?
Since it is a super beginner, there may be many mysterious codes, but thank you.

  • Answer # 1

    It seems that the parameters sent from view and the description of order_params do not match.
    If you look at the log, you can see the parameters, so compare them.

    Also, it seems that the parameters do not include information about "what to buy and how much". Is it unnecessary because I have it separately?

  • Answer # 2

    class OrderAddress
      include ActiveModel :: Model
      attr_accessor: postal_code,: prefecture_id,: city,: address_num,: building_name,: phone,: item_id,: token,: user_id
      with_options presence: true do
        validates: postal_code, format: {with:/\ A [0-9] {3}-[0-9] {4} \ z /, message: "is invalid. Include hyphen (-)"}
        validates: city, format: {with:/\ A [a-n-n-ichi- 龥] /, message: "is invalid. Input full-width characters."}
        validates: address_num
        validates: phone, format: {with:/\ A \ d {10} \ z | \ A \ d {11} \ z /, message: "is invalid."}
        validates: token
      end
        validates: prefecture_id, numericality: {other_than: 0, message: "can't be blank"}
        def save
          order = Order.create (user_id: user_id, item_id: item_id)
          Address.create (postal_code: postal_code, prefecture_id: prefecture_id, city: city, address_num: address_num, building_name: building_name, phone: phone, order_id: order.id)
        end
    end