我正在构建一个rails e commerce app,我的所有价格都是以美元计价,但现在我的客户想要以欧元显示价格。
客户希望能够为每种货币设定不同的价格,因此我无法将美元金额与欧元兑美元汇率挂钩以显示价格。
换句话说,客户必须能够输入两个输入价格,一个用于美元,一个用于欧元,然后显示在产品页面上。
我想要做的唯一方法是向数据库添加另一列并将其称为price_euros
或类似的东西。
由于我缺乏经验,我想在这里询问是否有另一种更简单的方法可以做到这一点?
提前致谢 d
这是views / products
中的show.html.erb
<div class="row product_top">
<div class="container">
<% breadcrumb :products %>
<div class="col-xs-12 col-sm-6 center-block">
<div class="product_description">
<h3><%= @product.name %></h3>
<h4><%#=@product.material %></h4>
<p><%= @product.description %></p>
<p class="price"> Designer: </p>
<p><%= @product.designer.designer_name %></p>
<p>In stock: <%= @product.stock_quantity %> </p>
</div>
<div class="col-xs-12">
<p> Price: <%= number_to_currency @product.price, class: 'price' %></p>
</div>
<div class="row add_to_cart">
<p><%= link_to 'Add to cart', add_cart_path, method: :post %></p>
</div>
</div>
<div class="col-xs-12 col-sm-6 center-block" >
<%= image_tag @product.image.url(:medium), class: "img-responsive" %>
</div>
<p>
<% if current_user && current_user.admin? %>
<%= link_to 'Edit', edit_product_path(@product) %> |
<% end %>
<%= link_to 'Back', root_path %>
</p>
</div>
</div>
这是管理员输入价格的_form.html.erb
部分
<%= form_for @product, multipart: true do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="container">
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control", rows: "5" %>
</div>
<div class="form-group">
<%= f.label :designer %><br>
<%= f.select :designer_id, Designer.all.map { |c| [ c.designer_name, c.id ] } %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.text_area :description, class: "form-control", rows: "5" %>
</div>
<div class="form-group">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="form-group">
<%= f.label :stock_quantity %><br>
<%= f.number_field :stock_quantity %>
</div>
<div class="form-group">
<%= f.label :category %><br>
<%= f.select :category_id, Category.all.map { |c| [ c.name, c.id ] } %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
这是我的产品型号product.rb
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
validates_presence_of :name, :price, :stock_quantity
validates_numericality_of :price, :stock_quantity
belongs_to :designer
belongs_to :category
belongs_to :page
def self.search(query)
where("name LIKE ? OR description LIKE ?", "%#{query}%", "%#{query}%")
end
end
这是product_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_filter :initialize_cart
before_action :authenticate_admin!, only: [ :new, :edit, :update, :create, :destroy ]
# GET /products
# GET /products.json
def index
@products = Product.all
end
def search
@products = Product.search(params[:query]).order("created_at DESC")
@categories = Category.joins(:products).where(:products => {:id => @products.map{|x| x.id }}).distinct
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :price, :image, :category_id, :stock_quantity, :designer_id, :query)
end
end