首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError ActionController

NameError ActionController
EN

Stack Overflow用户
提问于 2015-05-14 01:19:57
回答 1查看 62关注 0票数 0

我收到以下代码中的错误消息: NameError in PurchasesController#create未初始化常量购买::PurchaseCodeID

purchase_controller.rb

代码语言:javascript
复制
class PurchasesController < ApplicationController

    def create
        #if @memberTier = PurchaseCode.find_by(Code: 123456).Code then
            @purchase=Purchase.create(purchase_params)
            redirect_to :back
        #end
    end

    def index
        @purchases = Purchase.where(UserID: current_user.id)
        @sum = Purchase.where(UserID: current_user.id).sum(:Points)
        @redemp = Redemption.where(UserID: current_user.id).sum(:RedemptionID)
        @deff = RedemptionDetail.where(RedemptionID: @redemp).sum(:Points)
        @ans = @sum - @deff
    end

    def new
        @purchase = Purchase.new(purchase_params)
    end

    def purchase_params
        params.require(:purchase).permit(:PurchaseID,:PurchaseCodeID,:Points, :UserID)
    end



end

purchase.rb

代码语言:javascript
复制
class Purchase < ActiveRecord::Base
     validates :PurchaseCodeID, presence: true,
                    length: { minimum: 5 }


    def self.create(purchase_params)

        begin
            @purchaseid = Purchase.last.id
            @purchaseid = @purchaseid + 1
        rescue
            @purchaseid = 1 
        end
        @code = PurchaseCode.find_by(Code: PurchaseCodeID).Code
        @purchase = Purchase.new(purchase_params)
        @purchase.PurchaseCodeID = @code
        @purchase.PurchaseID = @purchaseid
        @purchase.UserID = User.current.id
        @userTier = User.current.TierID
        @tierPoint = MembershipTier.find_by(TierID: @userTier).RequiredPoint
        @purchase.Points = @tierPoint
        @purchase.save


    end





end

view/purchases/index.html.erb

代码语言:javascript
复制
<h1>Purchase History</h1>
<p>


  Enter the code here:</br>

<%= form_for :purchase, url: purchases_path do |f| %>

  <p>
    <%= f.text_field :PurchaseCodeID %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
<table>
  <thead>
    <tr>
      <th>Points</th>
      <th>Date</th>
      <th>Time</th>
      <th colspan="3"></th>
    </tr>

  </thead>

<tbody>
  <% @purchases.each do |purchase| %>

      <tr>
         <td><%= purchase.Points %></td>
         <td>|<%= purchase.created_at.to_s.to_date.strftime("%e %B, %Y") %></td>
         <td><%= purchase.created_at.to_s.to_time.strftime("| %I:%M %P") %></td>
      </tr>



  <% end %>

  </tbody>
</table>

  <tr>
  <td><p>Current Points: <%= @ans %></p></td>
  </tr>


<%= link_to t('sign_out'), destroy_user_session_path, method: :delete %>

我正在使用rails 4,希望有人能帮助我解决我的问题。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-14 02:29:19

PurchaseCodeID似乎是在purchase_params中,所以您应该修改这一行:

代码语言:javascript
复制
@code = PurchaseCode.find_by(Code: PurchaseCodeID).Code

至:

代码语言:javascript
复制
@code = PurchaseCode.find_by(Code: purchase_params[:PurchaseCodeID]).Code
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30227767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档