2016-10-08 16 views
0

私は以下のレールコードを持っていますRails:ビューからの関係属性へのアクセス方法

従業員モデル:id | emp_name | EMPLOYEE_ID | | visitor_name | vis_company | VIS |メール

class Visitor < ActiveRecord::Base 
    belongs_to :employee 
end 

従業員コントローラーID:

class EmployeesController < ApplicationController 
    before_action :set_employee, only: [:show, :edit, :update, :destroy] 

    # GET /employees 
    # GET /employees.json 
    def index 
    @employees = Employee.all 
    end 

    # GET /employees/1 
    # GET /employees/1.json 
    def show 
    end 

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

    # GET /employees/1/edit 
    def edit 
    end 

    # POST /employees 
    # POST /employees.json 
    def create 
    @employee = Employee.new(employee_params) 

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

    # PATCH/PUT /employees/1 
    # PATCH/PUT /employees/1.json 
    def update 
    respond_to do |format| 
     if @employee.update(employee_params) 
     format.html { redirect_to @employee, notice: 'Employee was successfully updated.' } 
     format.json { render :show, status: :ok, location: @employee } 
     else 
     format.html { render :edit } 
     format.json { render json: @employee.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /employees/1 
    # DELETE /employees/1.json 
    def destroy 
    @employee.destroy 
    respond_to do |format| 
     format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_employee 
     @employee = Employee.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def employee_params 
     params.require(:employee).permit(:emp_id, :emp_name, :emp_email, :emp_phone, :emp_mobile) 
    end 
end 

ビジターコントローラー:

class VisitorsController < ApplicationController 
    before_action :set_visitor, only: [:show, :edit, :update, :destroy] 

    # GET /visitors 
    # GET /visitors.json 
    def index 
    #@visitors = Visitor.find(:all, :order => 'emp_name') 
    #@visitors = Visitor.all.includes(:emp_name) 
    @visitors = Visitor.all 
    #@employees = @visitors.Employee.find(:all, :order => 'emp_name') 
     #@employees = @visitors.employee :include => [:emp_name] 
    end 

    # GET /visitors/1 
    # GET /visitors/1.json 
    def show 
    end 

    # GET /visitors/new 
    def new 
    @visitor = Visitor.new 
    end 

    # GET /visitors/1/edit 
    def edit 
    end 

    # POST /visitors 
    # POST /visitors.json 
    def create 
    @visitor = Visitor.new(visitor_params) 

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

    # PATCH/PUT /visitors/1 
    # PATCH/PUT /visitors/1.json 
    def update 
    respond_to do |format| 
     if @visitor.update(visitor_params) 
     format.html { redirect_to @visitor, notice: 'Visitor was successfully updated.' } 
     format.json { render :show, status: :ok, location: @visitor } 
     else 
     format.html { render :edit } 
     format.json { render json: @visitor.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /visitors/1 
    # DELETE /visitors/1.json 
    def destroy 
    @visitor.destroy 
    respond_to do |format| 
     format.html { redirect_to visitors_url, notice: 'Visitor was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_visitor 
     @visitor = Visitor.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def visitor_params 
     params.require(:visitor).permit(:vis_id, :vis_name, :vis_email, :vis_company, :employee_id) 
    end 
end 

class Employee < ActiveRecord::Base 

    has_many :visitors 
end 

ビジターモデルEMP_NUMBER私の主な問題はth

+0

どうしてですか?これは動作するはずです​​<%= visitor.employee.emp_name%> – Ursus

+0

訪問者に従業員はいますか?この場合、従業員は「nil」である可能性があります。 – djothefou

+0

それは私のために働いていないのですか?どうしてか分かりません! –

答えて

0

新しいプロジェクトを最初から作成して何らかの理由で作業を開始しました。私の間違いは、最初から関係を定義していないことでした。他のすべてが作成された後に私はemployee_idを追加しました。その時、レールは関係を構築しなかったと思います。おかげさまで皆様

+0

テーブルを削除し、次のコード "rake db:drop db:create db:migrate"をユーザーに再作成する必要があります。 –

関連する問題