1

我最近刚刚尝试实施“条纹”以在我的网站上进行付款。但是,我在尝试更新产品价格时遇到了一定的问题。

这是我的付款控制器。

class PaymentsController < ApplicationController
  def create
    token = params[:stripeToken]
    @product = Product.find(params[:product_id])
    @user = current_user

    # Create the charge on Stripe's servers to charge the user's card
    begin
      charge = Stripe::Charge.create(
        amount: (@product.price*100).to_i,
        currency: "eur", 
        source: token,
        description: params[:stripeEmail]
      )

      if charge.paid
        Order.create!(product_id: @product.id, user_id: @user.id, total: @product.price.to_i)
        UserMailer.order_placed(@user,@product).deliver_now
      end

    rescue Stripe::CardError => e
      body = e.json_body
      err = body[:error]
      flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
    end

    redirect_to product_path(@product), notice: 'Thank you for your order!'
  end
end

我的产品型号

class Product < ApplicationRecord
  has_many :orders
  has_many :comments
  validates :name, presence: true
  validates :price, presence: true

  def price_show
    "€ %.2f" % (self[:price]/100.0)
  end

  def price
    self[:price]
  end

  def self.search(search_term)
   Product.where("name LIKE ?", "%#{search_term}%")
end

# Called by <%= @product.highest_rating_comment %>
  def highest_rating_comment
   comments.rating_desc.first
 end

 def average_rating
  comments.average(:rating).to_f
 end
end

和我的部分形式。

<%= form_with(model: @product, local: true) do |form| %>
  <% 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="field">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>

  <div class="field">
    <%= form.label :description %>
    <%= form.text_area :description %>
  </div>

  <div class="field">
    <%= form.label :image_url %>
    <%= form.text_field :image_url %>
  </div>

  <div class="field">
  <%= form.label :color %><br>
  <%= form.text_field :color %>
  </div>

  <div class="field">
    <%= form.label :price %>
    <%= form.text_field :price %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

我遇到的问题是,当我输入价格时,这两行都会出现“未定义错误”,它会说“你的意思是@product吗?”。

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| %>

我相应地更改了这两行,页面加载。但是,当我尝试在页面上输入价格时,我会收到一条错误消息,指出价格“不能为空白”,即使我在字段中输入了一个数值。

我的产品控制器代码如下。

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    if params[:q]
      search_term = params[:q]
      @products = Product.search(search_term)
    else
    @products = Product.limit(4)
  end
end
  # GET /products/1
  # GET /products/1.json
  def show
    @comments = @product.comments.order("created_at DESC")
    @comments = @comments.paginate(:page => params[:page], :per_page => 2)
  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, :image_url, :color)
    end
end

如果这是一个愚蠢的问题,我对 Rails 比较陌生,很抱歉,但任何帮助将不胜感激。

4

1 回答 1

2

价格“不能为空”

您应该在方法中加入白名单 以修复验证错误(即价格“不能为空白”)并能够成功保存产品及其价格。priceproduct_params

def product_params
  params.require(:product).permit(:name, :description, :image_url, :color, :price)
end

错误原因:

由于您没有price将属性列入白名单,因此它将被视为未经许可的参数,并且params在保存@product. 并且作为save触发验证,验证检查@product 失败price导致该错误。

于 2018-09-18T10:05:09.090 回答