Javascript Rails通讯簿

Javascript Rails通讯簿,javascript,html,ruby-on-rails,json,ruby,Javascript,Html,Ruby On Rails,Json,Ruby,嗨,我有点傻,我很感激你能帮我做这件事。我正在尝试使用用户保存到他们通讯簿中的数据,之后我想让他们选择使用该地址进行传递 这是我的地址控制器 class AddressesController < ApplicationController before_action :authenticate_user! before_action :set_address, only: [ :show, :edit, :update, :destroy] # GET /addresses

嗨,我有点傻,我很感激你能帮我做这件事。我正在尝试使用用户保存到他们通讯簿中的数据,之后我想让他们选择使用该地址进行传递

这是我的地址控制器

class AddressesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_address, only: [ :show, :edit, :update, :destroy]

  # GET /addresses
  # GET /addresses.json
  def index
    @addresses = current_user.addresses.all
  end

  # GET /addresses/1
  # GET /addresses/1.json
  def show
  end

  # GET /addresses/new
  def new
    @address = current_user.addresses.build
  end

  # GET /addresses/1/edit
  def edit
  end

  # POST /addresses
  # POST /addresses.json
  def create
    @address = current_user.addresses.build(address_params)
    @address = current_user 

    respond_to do |format|
      if @address.save
        format.html { redirect_to addresses_url, notice: 'Address was successfully created.' }
        format.json { render :show, status: :created, location: @address }
      else
        format.html { render :new }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /addresses/1
  # PATCH/PUT /addresses/1.json
  def update
    respond_to do |format|
      if @address.update(address_params)
        format.html { redirect_to @address, notice: 'Address was successfully updated.' }
        format.json { render :show, status: :ok, location: @address }
      else
        format.html { render :edit }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /addresses/1
  # DELETE /addresses/1.json
  def destroy
    @address.destroy
    respond_to do |format|
      format.html { redirect_to addresses_url, notice: 'Address was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_address
      @address = Address.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def address_params
      params.require(:address).permit(:address, :latitude, :longitude)
    end
end
  before_action :authenticate_user!
  before_action :set_already_order, only: [:show, :edit, :update, :destroy]
  # GET /already_orders
  # GET /already_orders.json
  def index
    @already_orders = current_user.already_orders.order('created_at desc')
  end

  # GET /already_orders/1
  # GET /already_orders/1.json
  def show
  end
 

  # GET /already_orders/new
  def new
    @already_order = current_user.already_orders.build
  end

  # GET /already_orders/1/edit
  def edit
  end

  # POST /already_orders
  # POST /already_orders.json
  def create
    @already_order = current_user.already_orders.build(already_order_params)
    @already_order = current_user
    respond_to do |format|
      if @already_order.save
        format.html { redirect_to already_orders_url , notice: 'Already order was successfully created.' }
        format.json { render :show, status: :created, location: @already_order }
      else
        format.html { render :new }
        format.json { render json: @already_order.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /already_orders/1
  # PATCH/PUT /already_orders/1.json
  def update
    respond_to do |format|
      if @already_order.update(already_order_params)
        format.html { redirect_to already_orders_url, notice: 'Already order was successfully updated.' }
        format.json { render :show, status: :ok, location: @already_order }
      else
        format.html { render :edit }
        format.json { render json: @already_order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /already_orders/1
  # DELETE /already_orders/1.json
  def destroy
    @already_order.destroy
    respond_to do |format|
      format.html { redirect_to already_orders_url, notice: 'Already order was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_already_order
      @already_order = AlreadyOrder.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def already_order_params
      params.require(:already_order).permit(:shop_name, 
        :pick_up_time, 
        :shop_address, 
        :shop_district, 
        :delivery_time, 
        :item_type, 
        :item_cost, 
        :item_quantity, 
        :name, 
        :address, 
        :district, 
        :building, 
        :phone_number, 
        :specific_instructions, 
        :payment_method, 
        :cost,
        :deliver_id,
        :payment_status,
        :specify_delivery_time,
        :paid_for,
        :discount_code,
        :delivery_status,
        :cost_usd)
    end
end
这是我的订单

<%= form_for(@already_order) do |f| %>  
<div class="row p-3 pt-5">
    <div class="col-md-8 order-md-1">
       <h3 class="mb-3 text-muted">Pickup Details</h3>
        <div class="form-row">
          <div class="form-group form-group col-md-6">
            <label>Business name</label>
            <%= f.hidden_field :deliver_id, value: SecureRandom.hex(5) %>
            <%= f.hidden_field :payment_status, value: "Pending" %>
            <%= f.hidden_field :delivery_status %>
            <%= f.text_field :shop_name, class:"form-control", placeholder: "Grand Phenix", required: true %>
          </div>
          <div class="form-group col-md-6">
            <label>Business address</label>
            <%= f.text_field :shop_address, class:"form-control", placeholder: "Brades", required: true %>
          </div>
          <div class="form-group col-md-9">
            <label>Business district</label><em style="font-size: 12px;">  if business district and delivery district are diffrent you will be charged $4</em>
            <%= f.select :shop_district, ['','Cojoehead - Sweenyes', "St. John's - Look Out" , 'Cheapend - Salem'], {}, { :class => 'form-control', :id => 'dist1', :onchange => 'addto()' } %>
          </div>  
          <div class="form-group col-md-3">
            <%= f.label :pick_up_time %>
            <div class="form-inline">
              <%= f.datetime_select :pick_up_time, { ampm: true, order: [:hour, :minute] }, { class:"form-control", :required => true } %>
            </div>
          </div>
          <div class="form-group col-md-5">
            <label>Address</label>
            <%= f.select("address", Address.all.collect {|p| [ p.address ] },{}, { :class => 'form-control' })%>
          </div>
          <div class="form-group col-md-5">
            <label>Item Type</label>
            <%= f.select :item_type, ['','Food & Drinks', "Groceries" , 'Packages'], {}, { :class => 'form-control', :id => 'sitem', :onchange => 'addto()' } %>
          </div>
          <p id="item_cost"></p>
          <p id="dis_cost"></p>
          <div class="form-group col-md-4">
            <label>Item Quantity or Weight in lbs</label>
            <%= f.number_field :item_quantity, class: 'form-control' , id:'weight', :onchange => 'addto()' %>
          </div>
          <div class="form-group col-md-3">
            <%= f.label :delivery_time %>
            <div class="form-inline">
              <%= f.select :delivery_time, ['8:00 am', '11:50 am', '3:00 pm', '7:30 pm'], {}, { :class => 'form-control' } %>
            </div>
          </div>  
        </div><br><br><br>
        
        <h3 class="text-muted">Delivery Details </h3>
        <div class="form-row">
          <div class="form-group col-md-5">
            <%= f.label :name %>
            <%= f.text_field :name, class:"form-control", placeholder: "John Browne", required: true %>
          </div>
          <div class="form-group col-md-5">
          </div>
          <div class="form-group col-md-6">
            <%= f.label :building %>
            <%= f.text_field :building, class:"form-control", placeholder: "Third house on the right", required: true %>
          </div>
          <div class="form-group col-md-10">
            <%= f.label :district %><em style="font-size: 12px;">  if delivery district and shop district are diffrent you will be charged $4</em>
            <%= f.select :district, ['','Cojoehead - Sweenyes', "St. John's - Look Out" , 'Cheapend - Salem'], {}, { :class => 'form-control', :id => 'dist2', :onchange => 'addto()' } %>
          </div> 
          <div class="form-group col-md-4">
            <%= f.label :phone_number %>
            <%= f.number_field :phone_number, class:"form-control", placeholder: "393-0000", required: true %>
          </div>
          <div class="form-group col-md-10">
            <%= f.label :specific_instructions %>
            <%= f.text_area :specific_instructions, class:"form-control", placeholder: "Knock on the pink door downstairs and deliver it to Tim Browne ", :size => "24x6", :required => true %>
          </div>
        </div>
        <!-- Button trigger modal -->
        <div class="ml-3">
          <input class="form-check-input" type="checkbox" required>
          <button type="button" class="btn btn-info" data-toggle="modal" data-target="#staticBackdrop">
            Terms and Conditions
          </button>
        </div>
        <!-- Modal -->
        <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="staticBackdropLabel">Instructions and Pricing</h5>
              </div>
              <div class="modal-body">
                Please ensure that the Pickup district and delivery district are correct as you will be charged $5 for wrong details.<br><br>
                <div class="mx-2"><strong>Food and Drinks</strong> <br> 
                <em>1 Food and Drink is are a pair as 1</em><br>
                1-2 Food and Drinks = $5 <br>
                3-6 Food and Drinks = $10 <br>
                More and 6 Food and Drinks = $1.6 per <br>
                <strong>Groceries</strong> <br>
                $1.75 per lb <br>
                <strong>Items</strong> <br>
                $2.2 per lb </div><br><br>
                <h5>Paymant options</h5>
                <div class="mx-2"><strong>Already paid</strong> - The item is already paid for and it ready for pick up <br>
                <strong>Pay on arrival</strong> - This only applies for items under $50 where Deliver will pay for the item and apon delivery the amount will be paid back plus a $5 fee  </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Understood</button>
              </div>
            </div>
          </div>
        </div><br><br>
          
    </div>

    <div class="col-md-4 order-md-2 mb-4 text-danger">
      <h4 class="d-flex justify-content-between align-items-center mb-3">
        <span class="text-muted">Charges</span>
      </h4>
      <ul class="list-group mb-2">
        <li class="list-group-item d-flex justify-content-between lh-condensed">
          <p id="pitem"></p>
          <span class="text-muted" id="price"></span>
        </li>
        <li class="list-group-item d-flex justify-content-between lh-condensed">
          <p id="district"></p>
          <span class="text-muted" id="district_cost"></span>
        </li>
        <li class="list-group-item d-flex justify-content-between lh-condensed">
          <p id="payment"></p>
          <span class="text-muted" id="pay_cost"></span>
        </li>
        <li class="list-group-item d-flex justify-content-between lh-condensed">
          <p></p>
          <span class="text-muted"></span>
        </li>
      </ul><br>
      <span class="text-muted">Payment Method</span><br>
      <div>
        <%= f.select :payment_method, ['Paypal/Credit Card', "Pay on Arrival"], {}, { :class => 'form-control', :id => 'pay_opt', :onchange => 'addto()' } %>
      </div><br><br>
      <p id="ttl1"></p>
      <p id="ttl2"></p>
      <p id="ttl3"></p>
      <li class="list-group-item d-flex justify-content-between">
        <h5>Total (EC)</h5>
        <%= f.hidden_field :cost, id: "ptotal" %>
        <%= f.hidden_field :cost_usd, id: "usdtotal" %>
        <strong id="end"></strong>
      </li>
      <br>

        
      <div class="input-group">
      <%= f.hidden_field :discount_code, class: 'form-control' %>
        <input type="text" class="form-control" placeholder="Discount code">
        <div class="input-group-append">
          <p  class="btn btn-secondary">Redeem</p>
        </div>
      </div>
    </div>
  </div>
  <div class="form-actions">
    <%= f.button :Submit, class: "btn btn-success", id: 'display' %>
  </div> 
  <% end %>
</div>
<p id="pay_val"></p>
<p id="end2"></p>


接送详情
企业名称
营业地址
商业区如果商业区和配送区不同,将向您收取4美元
'表单控件',:id=>'dist1',:onchange=>'addto()'}%>
真}%>
地址
'表单控件'})%>
项目类型
'表单控件',:id=>'sitem',:onchange=>'addto()'}%>

项目数量或重量(磅) “addto()”%> “表单控件”}%>


送货详情 如果送货区和商店区不同,您将收取4美元 '表单控件',:id=>'dist2',:onchange=>'addto()'}%> “24x6”,必需=>true%> 条款和条件 说明和定价 请确保取货区和送货区正确无误,因为错误的详细信息将向您收取5美元。

食品和饮料
1食物和饮料是一对1
1-2食物和饮料=5美元
3-6食物和饮料=10美元
更多和6份食物和饮料=$1.6/份
食品杂货
每磅1.75美元
项目
每磅2.2美元

付费选项 已付款-该商品已付款,可以提货
货到付款-这仅适用于50美元以下的物品,其中Deliver将支付该物品的费用,而apon delivery将支付该金额加上5美元的费用 理解

收费

付款方式
'表单控件',:id=>'pay_opt',:onchange=>'addto()'}%>

  • 总数(欧共体)

  • 兑换

    我用它来显示已保存地址的选项

    <%= f.select("address", Address.all.collect {|p| [ p.address ] },{}, { :class => 'form-control' })%>
    
    “表单控件”})%>
    
    但是当我提交数据时,我得到一个关联未匹配错误,我也不认为代码按我所希望的那样工作,因为在提交订单后,我希望关联的地址名称、经度和纬度与它一起提交

    我的模特协会是

     class Address < ApplicationRecord
        belongs_to :user
        has_many :already_orders
        has_many :orders
    end
    
    类地址
    class-AlreadyOrder
    class用户

    我希望能在这个问题上得到任何帮助,但我认为问题在于:

    <%= f.select("address", Address.all.collect {|p| [ p.address ] },{}, { :class => 'form-control' })%>
    
    您希望阵列的外观如下所示:

    [['the value of the Address objects address attribute', address_object_id], ['the value of the Address objects address attribute', address_object_id]]
    
    Rails将遍历外部数组,并将每个内部数组的索引0用于HTML选项标记的innerText。它将使用每个内部数组的索引1作为HTML选项标记的值

    该行代码的固定版本如下:

    <%= f.select("address", Address.all.collect {|p| [ p.address, p.id ] },{}, { :class => 'form-control' })%>
    
    “表单控件”})%>
    
    lool。我已经试过了,但是出现了一个不同的错误
    Address(#110424960)预期,得到了“1”,这是String(#22281700
    的一个实例,这将指向地址控制器中的create方法
    <%= f.select("address", Address.all.collect {|p| [ p.address ] },{}, { :class => 'form-control' })%>
    
    [['the value of the Address objects address attribute'], ['the value of the Address objects address attribute']]
    
    [['the value of the Address objects address attribute', address_object_id], ['the value of the Address objects address attribute', address_object_id]]
    
    <%= f.select("address", Address.all.collect {|p| [ p.address, p.id ] },{}, { :class => 'form-control' })%>