Rails:如何从视图中访问关系属性

时间:2016-10-08 08:35:41

标签: ruby-on-rails ruby-on-rails-4

我有以下导轨代码

员工模型:id | emp_name | emp_number

class Employee < ActiveRecord::Base

    has_many :visitors
end

访客模型:id | employee_id | visitor_name | vis_company | vis | email

    class Visitor < ActiveRecord::Base
    belongs_to :employee
end

员工控制员:

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

现在我的主要问题是我无法从访客视图访问员工姓名:

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

<h1>Listing Visitors</h1>

<table>
  <thead>
    <tr>
      <th>Vis</th>
      <th>Vis name</th>
      <th>Vis email</th>
      <th>Vis company</th>
      <th>Visitor Host</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @visitors.each do |visitor| %>
      <tr>
        <td><%= visitor.id %></td>
        <td><%= visitor.vis_name %></td>
        <td><%= visitor.vis_email %></td>
        <td><%= visitor.vis_company %></td>
        <td><%= visitor.employee.emp_name %></td>
        <td><%= link_to 'Show', visitor %></td>
        <td><%= link_to 'Edit', edit_visitor_path(visitor) %></td>
        <td><%= link_to 'Destroy', visitor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Visitor', new_visitor_path %>

1 个答案:

答案 0 :(得分:0)

我从头开始创建新项目,由于某种原因,它开始工作。我的错误是我从一开始就没有定义关系。在创建了其他所有内容之后我添加了employee_id我认为rails当时没有构建关系。谢谢大家