0

我正在通过设计构建一个拥有一个“主用户”的应用程序。此主用户创建我们正在跟踪的员工和项目的记录。当用户想要“结账”时,他会向 twilio 电话号码发送一条文本,这将为该项目创建一个新的“交易”,要么签入要么签出。所以我有三个表——“员工”、“项目”和“交易”。计划是拦截这些传入的文本,解析它来自的电话号码,他们发短信的项目,以及他们给出的代码,即“CI 12345”将“签入项目 12345”等。无论如何,我我正在搭建这些东西,只是为了确保在我开始使用 API 来创建交易记录之前,我可以在 Web 应用程序中手动进行这些交易,但我很难将交易与项目(和/或用户)关联起来。交易需要有一个布尔“输入/输出”值、一个电话号码以将其与执行该操作的员工相匹配,然后是一个项目编号以将其与被跟踪的项目相关联。我不断收到错误消息,说我在尝试加载“交易显示视图”时没有为“交易”定义方法,可以这么说。我不确定这里发生了什么。这一切有点新,这只是我的第二个应用程序。然后是一个项目编号,将其与被跟踪的项目相关联。我不断收到错误消息,说我在尝试加载“交易显示视图”时没有为“交易”定义方法,可以这么说。我不确定这里发生了什么。这一切有点新,这只是我的第二个应用程序。然后是一个项目编号,将其与被跟踪的项目相关联。我不断收到错误消息,说我在尝试加载“交易显示视图”时没有为“交易”定义方法,可以这么说。我不确定这里发生了什么。这一切有点新,这只是我的第二个应用程序。

数据库/schema.rb

    ActiveRecord::Schema.define(:version => 20130515114928) do

      create_table "employees", :force => true do |t|
        t.string   "name"
        t.string   "phone"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
      end

      create_table "items", :force => true do |t|
        t.string   "assettag"
        t.string   "description"
        t.datetime "created_at",  :null => false
        t.datetime "updated_at",  :null => false
      end

      create_table "transactions", :force => true do |t|
        t.boolean  "status"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
        t.integer  "item_id"
        t.string   "empphone"
      end

      add_index "transactions", ["item_id"], :name => "index_transactions_on_item_id"

      create_table "users", :force => true do |t|
        t.string   "email",                  :default => "", :null => false
        t.string   "encrypted_password",     :default => "", :null => false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",          :default => 0
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
        t.datetime "created_at",                             :null => false
        t.datetime "updated_at",                             :null => false
      end

      add_index "users", ["email"], :name => "index_users_on_email", :unique => true
      add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

    end

应用程序/控制器/transactions_controller.rb

    class TransactionsController < ApplicationController
      # GET /transactions
      # GET /transactions.json
      def index
        @transactions = Transaction.all

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @transactions }
        end
      end

      # GET /transactions/1
      # GET /transactions/1.json
      def show
        @transaction = Transaction.find(params[:id])
        @description = transaction.item.description

        respond_to do |format|
          format.html # show.html.erb
          format.json { render json: @transaction }
        end
      end

      # GET /transactions/new
      # GET /transactions/new.json
      def new
        @transaction = Transaction.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @transaction }
        end
      end

      # GET /transactions/1/edit
      def edit
        @transaction = Transaction.find(params[:id])
      end

      # POST /transactions
      # POST /transactions.json
      def create
        @transaction = Transaction.new(params[:transaction])

        respond_to do |format|
          if @transaction.save
            format.html { redirect_to @transaction, notice: 'Transaction was successfully created.' }
            format.json { render json: @transaction, status: :created, location: @transaction }
          else
            format.html { render action: "new" }
            format.json { render json: @transaction.errors, status: :unprocessable_entity }
          end
        end
      end

      # PUT /transactions/1
      # PUT /transactions/1.json
      def update
        @transaction = Transaction.find(params[:id])

        respond_to do |format|
          if @transaction.update_attributes(params[:transaction])
            format.html { redirect_to @transaction, notice: 'Transaction was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: "edit" }
            format.json { render json: @transaction.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /transactions/1
      # DELETE /transactions/1.json
      def destroy
        @transaction = Transaction.find(params[:id])
        @transaction.destroy

        respond_to do |format|
          format.html { redirect_to transactions_url }
          format.json { head :no_content }
        end
      end
    end

应用程序/模型/item.rb

    class Item < ActiveRecord::Base
      attr_accessible :assettag, :description
      validates :assettag, presence: true
      validates :description, presence: true

      has_many :transactions
    end

应用程序/模型/employee.rb

    class Employee < ActiveRecord::Base
      attr_accessible :name, :phone
      validates :phone, presence: true
      validates :name, presence: true
    end

应用程序/模型/transaction.rb

    class Transaction < ActiveRecord::Base
      attr_accessible :status, :item_id

      belongs_to :item
      validates :item_id, presence: true

      delegate :description, to: :item
    end

应用程序/控制器/employees_controller.rb

    class EmployeesController < ApplicationController
      # GET /employees
      # GET /employees.json
      def index
        @employees = Employee.all

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @employees }
        end
      end

      # GET /employees/1
      # GET /employees/1.json
      def show
        @employee = Employee.find(params[:id])

        respond_to do |format|
          format.html # show.html.erb
          format.json { render json: @employee }
        end
      end

      # GET /employees/new
      # GET /employees/new.json
      def new
        @employee = Employee.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @employee }
        end
      end

      # GET /employees/1/edit
      def edit
        @employee = Employee.find(params[:id])
      end

      # POST /employees
      # POST /employees.json
      def create
        @employee = Employee.new(params[:employee])

        respond_to do |format|
          if @employee.save
            format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
            format.json { render json: @employee, status: :created, location: @employee }
          else
            format.html { render action: "new" }
            format.json { render json: @employee.errors, status: :unprocessable_entity }
          end
        end
      end

      # PUT /employees/1
      # PUT /employees/1.json
      def update
        @employee = Employee.find(params[:id])

        respond_to do |format|
          if @employee.update_attributes(params[:employee])
            format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: "edit" }
            format.json { render json: @employee.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /employees/1
      # DELETE /employees/1.json
      def destroy
        @employee = Employee.find(params[:id])
        @employee.destroy

        respond_to do |format|
          format.html { redirect_to employees_url }
          format.json { head :no_content }
        end
      end
    end

应用程序/控制器/items_controller.rb

    class ItemsController < ApplicationController
      # GET /items
      # GET /items.json
      def index
        @items = Item.all

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @items }
        end
      end

      # GET /items/1
      # GET /items/1.json
      def show
        @item = Item.find(params[:id])

        respond_to do |format|
          format.html # show.html.erb
          format.json { render json: @item }
        end
      end

      # GET /items/new
      # GET /items/new.json
      def new
        @item = Item.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @item }
        end
      end

      # GET /items/1/edit
      def edit
        @item = Item.find(params[:id])
      end

      # POST /items
      # POST /items.json
      def create
        @item = Item.new(params[:item])

        respond_to do |format|
          if @item.save
            format.html { redirect_to @item, notice: 'Item was successfully created.' }
            format.json { render json: @item, status: :created, location: @item }
          else
            format.html { render action: "new" }
            format.json { render json: @item.errors, status: :unprocessable_entity }
          end
        end
      end

      # PUT /items/1
      # PUT /items/1.json
      def update
        @item = Item.find(params[:id])

        respond_to do |format|
          if @item.update_attributes(params[:item])
            format.html { redirect_to @item, notice: 'Item was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: "edit" }
            format.json { render json: @item.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /items/1
      # DELETE /items/1.json
      def destroy
        @item = Item.find(params[:id])
        @item.destroy

        respond_to do |format|
          format.html { redirect_to items_url }
          format.json { head :no_content }
        end
      end
    end

app/views/transactions/show.html.erb

    <p id="notice"><%= notice %></p>

    <p>
      <b>Status:</b>
      <%= @transaction.status %>
    </p>

    <p>
      <b>Description:</b>
      <%= transaction.description %>
    </p>


    <%= link_to 'Edit', edit_transaction_path(@transaction) %> |
    <%= link_to 'Back', transactions_path %>
4

1 回答 1

1

在您的 transactions_controller.rb 中,删除您分配@description实例变量的行。

在您的交易/show.html.erb 中:

<p>
  <b>Description:</b>
  <%= @transaction.description.presence || "No description" %>
</p>
于 2013-05-15T14:15:20.673 回答