首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >4表数据库的关联、Belongs_to和所有权

4表数据库的关联、Belongs_to和所有权
EN

Stack Overflow用户
提问于 2013-05-15 22:02:37
回答 1查看 51关注 0票数 0

我正在构建一个通过devise拥有一个“主用户”的应用程序。这个主用户创建我们正在跟踪的员工和项目的记录。当用户想要“结帐商品”时,他会向twilio电话号码发送一条文本,这将为该商品创建一个新的“交易”,无论是签入还是签出。所以我有三个表- 'employees','items‘和'transactions’。计划是拦截这些传入的文本,解析其来源的电话号码,他们正在发送短信的项目,以及他们给出的代码,即“'CI 12345”将“签入项目12345”,等等。不管怎样,我搭建了这些东西,只是为了确保在我开始使用API创建交易记录之前,我可以在web应用程序中手动进行这些交易,但我很难将交易与项目(和/或用户)关联起来。交易需要有一个布尔值'in/out‘,一个电话号码使其与执行该操作的员工相匹配,然后有一个项目编号将其与被跟踪的项目相关联。可以这么说,当我试图加载一个“事务显示视图”时,我一直收到错误,说我没有为“事务”定义一个方法。我不知道这是怎么回事。这对我来说有点陌生,这只是我的第二个应用。

db/schema.rb

代码语言:javascript
复制
    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_Controler.rb

代码语言:javascript
复制
    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

app/model/item.rb

代码语言:javascript
复制
    class Item < ActiveRecord::Base
      attr_accessible :assettag, :description
      validates :assettag, presence: true
      validates :description, presence: true

      has_many :transactions
    end

app/models/employee.rb

代码语言:javascript
复制
    class Employee < ActiveRecord::Base
      attr_accessible :name, :phone
      validates :phone, presence: true
      validates :name, presence: true
    end

app/model/transaction.rb

代码语言:javascript
复制
    class Transaction < ActiveRecord::Base
      attr_accessible :status, :item_id

      belongs_to :item
      validates :item_id, presence: true

      delegate :description, to: :item
    end

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

代码语言:javascript
复制
    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_Controler.rb

代码语言:javascript
复制
    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

应用程序/视图/事务/show.html.erb

代码语言:javascript
复制
    <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 %>
EN

回答 1

Stack Overflow用户

发布于 2013-05-15 22:15:21

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

在您的事务/show.html.erb中:

代码语言:javascript
复制
<p>
  <b>Description:</b>
  <%= @transaction.description.presence || "No description" %>
</p>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16567198

复制
相关文章

相似问题

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