2014-04-09 59 views
0

我遇到了一些不屬於我的模型的屬性的問題。我正在設計一個簡單的付款頁面,如下所示,所有的付款都在我的數據庫中表示,除了card_number和card_verification出於安全原因。Rails 4 - 不屬於模型中的一部分的字段

當我加載它拋出一個錯誤頁面:

undefined method `card_number' for #<Order:0x00000004eddb00> 
undefined method `card_verification' for #<Order:0x00000004eddb00> 

這有什麼錯我的形式?

謝謝。

的形式:

<%= simple_form_for(@order, html:{class: "well"}) do |f| %> 
    <%= f.input :first_name %> 
    <%= f.input :last_name %> 
    <%= f.input :card_type, collection: ["Visa", "MasterCard"] %> 
    <%= f.input :card_expires_on %> 
    <%= f.input :card_number %> 
    <%= f.input :card_verification %> 
    <%= f.button :submit %> 
<% end %> 

order.rb:

class Order < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :participation 
end 

orders_controller.rb:

class OrdersController < ApplicationController 
    before_action :set_order, only: [:show, :edit, :update, :destroy] 

    def new 
    @order = Order.new 
    end 

    def create 
    @order = Order.new(order_params) 
    respond_to do |format| 
     if @order.save 
     format.html { redirect_to @order, notice: 'Order was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @order } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @order.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    private 
    def set_order 
     @order = Order.find(params[:id]) 
    end 

    def order_params 
     params.require(:order).permit(:ip_address, :first_name, :last_name, :card_type, :card_expires_on, :user_id, :participation_id) 
    end 
end 
+0

卡號不允許?好的,我知道了。那麼,爲什麼不直接使用html助手來處理這些數據呢? – zishe

回答

2

我薄K表拋出的錯誤應該是這樣的表單中的一部分:

<%= f.input :card_number %> 
<%= f.input :card_verification %> 

你可以嘗試添加:

attr_accessor :card_number, :card_verification 

到模型。

+0

Rails4中的attr_accessor不是折舊嗎?我使用Rails4,檢查問題的標題。 – msdundar

+0

'attr_accessor'是Ruby方法。不要將它與'attr_accessible'混淆。 –

+0

是嗎?我想你在考慮attr_accessible ... – user3334690

相關問題