【问题标题】:Change a variable in form based on selection within form根据表单中的选择更改表单中的变量
【发布时间】:2012-06-14 17:54:59
【问题描述】:

2012 年 6 月 21 日更新

我在 Rails 中有一个类似于电子商务结帐的表单。

用户在表单中选择开始时间、结束时间、日期、时间和价格偏好(每小时、每周、每天等)。该产品已经在数据库中设置了我要转换的价格(每小时、每周、每天等),我想根据价格偏好进行更改,然后与表单的其余部分一起提交。

我一直在关注 Ryan 在 dynamic select menus 上的截屏视频,但我不确定如何将他的演示应用到我的应用程序中,因为我不希望用户只选择价格(每天/每小时/等)

装备 has_many line_items line_items 属于 Gear 和 Carts

下面是我的代码:

齿轮模型(我在其中创建了价格变量)

class Gear < ActiveRecord::Base
  attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :remote_image_url, :color, :year, :latefee, :cancellation, :minrental, :policy, :about, :address, :city, :state, :zip
  belongs_to :user
  belongs_to :sub_category
  has_one :category, :through => :sub_category
  has_many :comments, :dependent => :destroy 
  has_many :line_items
  has_many :calendars
  require 'carrierwave/orm/activerecord'
  mount_uploader :image, GearpicUploader
  mount_uploader :image_a, GearpicUploader
  before_destroy :ensure_not_referenced_by_any_line_item

  validates :title, presence: true
  validates :size,  presence: true
  validates :price, presence: true
  validates :sub_category_id, presence: true
  validates :user_id, presence: true

  def hourly_price
    price/24
  end

  def weekly_price
    price*7
  end

  def monthly_price
    weekly_price*4
  end
end

查看

<div class="gearside_date_top">
    <%= form_for LineItem.new do |f| %>
    <p><%= render :partial => "price" %> / 
        <%= f.select :day_or_hour, [['day', 1], ['hour', 2]], :id => 'day_or_hour' %>
    </p>
</div>      

部分给予查看

<em>from</em>
<span id="gear_daily" style="font-size: 190%; color: #94cbfc;">$<%= @gear.price %></span>
<span id="gear_hourly" style="font-size: 190%; color: #94cbfc; display:none;">$<%= @gear.hourly_price %></span>

Javascript

$('#day_or_hour').change(function() {

     var $index = $('#day_or_hour').index(this);

     if($('#day_or_hour').val() = '2') { 
     $('#gear_hourly').show(); 
     } 
     else {
     $('#gear_daily').show();//else it is shown

     }
});

我似乎无法让它工作。

【问题讨论】:

    标签: javascript ruby-on-rails forms


    【解决方案1】:

    你可以把你的价格放在一个部分:

    <div class="gearside_date_top">
      <%= form_for LineItem.new do |f| %>
      <%= render :partial => "price" %>
      <%= f.select :day_or_hour, [['day', 1], ['hour', 2]], :id => 'day_or_hour' %>
    

    _price.html.erb

    <em>from</em><span style="font-size: 190%; color: #94cbfc;">$<%= @gear.price %></span>
    

    在您的 line_item.js 中,您输入了更新价格的代码: line_item.js

    $('#day_or_hour').change(function() {
      $.get("/cart/update_price", {option: $(this).val()}, function(data) {
      }
    }
    

    cart_controller.rb 上,您创建调用 line_item.rb total_price 的方法 update_price。

    希望你能理解。

    编辑

    它看起来像这样。

    def update_price
      @gear.price = LineItem.total_price(@gear)
      render :partial => 'price'
    end
    

    【讨论】:

    • 您能否详细介绍 update_price 方法的外观,以及如何通过表单发送该变量?
    • 添加了使用 update_price 方法的编辑。此代码只是为了让您了解代码的外观。
    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 2014-05-03
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多